How to Get key value pair tuple array in swift?
Asked Answered
T

3

8

I have pairs tuple array pickerDataVisitLocation.just I want to know how to return key value pair location from my array using uniqId ex 204

var pickerDataVisitLocation:[(uniqId:Int,location:String)] = [(203,"Home"),(204,"Hospital"),(205,"Other")]
var selectedIndex = pickerDataVisitLocation[1].uniqId
pickerDataVisitLocation[selectedIndex].location //<--fatal error: Index out of range
Troll answered 13/10, 2016 at 19:46 Comment(4)
So selectedIndex is 204, as that's the uniqId of item 1 in your array. Then you try to get pickerDataVisitLocation[204], but it only has 3 items. What's the problem? What do you expect it to do?Alleris
Is a tuple even needed? I would just do a dictionary of [Int:String]Cincinnati
what I want is to get pair value location"Hospital" that have uniqId is 204Troll
selectedIndex will be the one which you have given i.e 203,204,205 according to this code, and your array only has 3 elementsAllonym
A
19

Make use of Sequence's first(where:) method

You could make use of Sequence's first(where:) to access the first tuple element of the array that meets a boolean requirement based on the first member of the tuple elements (uniqId). For the resulting tuple element, simply access the second member of the tuple (location).

var pickerDataVisitLocation: [(uniqId: Int, location: String)] = 
    [(203, "Home"), (204, "Hospital"), (205, "Other")]

// say for a given uniqId 204
let givenId = 204
let location = pickerDataVisitLocation
               .first{ $0.uniqId == givenId }?.location ?? ""
print(location) // Hospital

If no tuple element can be found for a given id, the method above will result in a resulting string that is empty (due to the nil coalescing operator). As an alternative, you could use an optional binding clause to proceed only for a non-nil return from .first:

var pickerDataVisitLocation: [(uniqId:Int,location:String)] = 
    [(203,"Home"),(204,"Hospital"),(205,"Other")]

// say for a given uniqId 204
let givenId = 204
if let location = pickerDataVisitLocation
                  .first(where: { $0.uniqId == givenId })?.location {
    print(location) // Hospital
}

Possibly an alternative: consider using a dictionary

Finally, since the first member of you tuple elements, uniqId, hints at unique members, and its type Int being Hashable, you might want to consider making use of a dictionary rather than an array of tuples. This will ease access of values associated with give unique Int id's, but you will loose the ordering of "elements" (key-value pairs) in the dictionary, as dictionaries are unordered collections.

var pickerDataVisitLocation = [203: "Home", 204: "Hospital", 205: "Other"]

// say for a given uniqId 204
let givenId = 204
if let location = pickerDataVisitLocation[givenId] {
    print(location) // Hospital
}
Ammonia answered 13/10, 2016 at 20:11 Comment(3)
I wish I can do more UpVoteTroll
@Troll happy to help :)Ammonia
Hey @NinjaDeveloper, its been some years since this. I'm new to swift, how can I access more values from the struct instead of just one "?.location", what if I want all of the values corresponding.Lockup
A
3

According to the given code:
Try this

var pickerDataVisitLocation:[(uniqId:Int,location:String)] = [(203,"Home"),(204,"Hospital"),(205,"Other")]
let selectedIndex = pickerDataVisitLocation[1].uniqId
var location = ""

for item in pickerDataVisitLocation {
    if item.uniqId == selectedIndex {
        location = item.location
    }
}

print(location) //Will print Hospital here
Allonym answered 13/10, 2016 at 19:57 Comment(0)
S
1

You could try something like below.

extension Array {
    func tupleWithId(id: Int) -> (uniqId:Int,location:String)? {
        let filteredElements = self.filter { (tuple) -> Bool in
            if let tuple = tuple as? (uniqId:Int,location:String) {
                return tuple.uniqId == id
            }
            return false
        }

        if filteredElements.count > 0 {
            let element = filteredElements[0] as! (uniqId:Int,location:String)
            return element
        }
        return nil
    }
}
var pickerDataVisitLocation:[(uniqId:Int,location:String)] = [(203,"Home"),(204,"Hospital"),(205,"Other")]
var selectedIndex = pickerDataVisitLocation[1].uniqId
pickerDataVisitLocation.tupleWithId(id: selectedIndex)?.location
Swab answered 13/10, 2016 at 20:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.