user10376561
user10376561

Reputation:

Swift array with another array reference to get values

My scenario, I have two separate array with same count. I am showing array_one into my picker or table. I don’t show array_two but array_one I will show in table or picker view. when user click particular index I can get array_one value but same time I need to get relevant ID(array_two) also. How to do that, Please provide some sample.

   Var array_one = [“Hindi”,”English”,”Bengali”,”Telugu”,”Odia”]
   Var array_two = [“05”,”02”,”08”,”02”,”09”]

Above, array I have language and ID. I am listing language in picker or tableview when i select language relevant index ID also I need to get. Output like: “Hindi, 05"

Upvotes: 1

Views: 1417

Answers (3)

meggar
meggar

Reputation: 1219

You can use a computed property that zips the two arrays:

var languages = { return zip(array_one, array_two).map{ "\($0.0), \($0.1)" } }

This will zip the 2 arrays, and map each pair of values to a comma separated string. Since it is computed, any changes to the original arrays will automatically appear. So now you can use languages as the data source.

Upvotes: 1

Caleb
Caleb

Reputation: 125037

I have two separate array with same count.

Trying to keep two or more arrays synchronized is never a good idea -- it's too easy to add or remove something from one array and forget to update the other(s), and that makes this approach a constant source of serious bugs.

A better way to do it is to keep all the corresponding pieces of data together in some sort of data structure, and store those structures in a single array. That eliminates the possibility of forgetting to update all the arrays, because there's only one to worry about. Different languages support this approach in different ways; Swift gives us tuples. You can define your array like this:

var array = [("Hindi", "01"),
             ("English", "02"),
             ("Bengali", "03"),
             ("Telugu", "04),
             ("Odia", "05")]

Now you've got both sets of data in a single array, and you can access the parts separately:

let third_language = array[3].0
let third_id = array[3].1

But you can also name the parts of the tuple if you want:

var array : [(language:String, id:string)] = [("Hindi", "01"),
                                              ("English", "02"),
                                              ("Bengali", "03")]
let third = array[3]
print(third.language)    // prints "Bengali"
print(third.id)          // prints "03"

Upvotes: 0

ielyamani
ielyamani

Reputation: 18591

Try this:

let array_one = ["Hindi","English","Bengali","Telugu","Odia"]
let array_two = ["01","02","03","04","05"]

let language = "Hindi"

guard let index = array_one.index(of: language) else {
    fatalError("Couldn't find the language")
}

let languageId = array_two[index]

let output = language + ", " + languageId


print(output) //"Hindi, 01"

Actually, array_two is not needed:

let array_one = ["Hindi","English","Bengali","Telugu","Odia"]

let language = "Hindi"

guard let index = array_one.index(of: language) else {
    fatalError("Couldn't find the language")
}

let languageId = String(format: "%02d", index + 1)

let output = language + ", " + languageId 

Here is a solution using zip:

let array_one = ["Hindi","English","Bengali","Telugu","Odia"]
let array_two = ["01","02","03","04","05"]
let zipped = zip(array_one, array_two)

let language = "Hindi"

guard let index = array_one.index(of: language),
    let languageAndId = zipped.first(where: {$0.0 == language
    })
else {
    fatalError("Couldn't find the language")
}

let output = languageAndId.0 + ", " + languageAndId.1

print(output)  //prints "Hindi, 01"

And to have an Object-Oriented Programming approach, use structs:

let array_one = ["Hindi","English","Bengali","Telugu","Odia"]
let array_two = ["01","02","03","04","05"]

struct Language {
    let name: String
    let index: String
}

let languagesArray: [Language] = zip(array_one, array_two).map{Language(name: $0.0, index: $0.1)}

let language = "Hindi"

guard let languageAndId = languagesArray.first(where: {$0.name == language
    }) else {
    fatalError("Couldn't find the language")
}

let output = languageAndId.name + ", " + languageAndId.index

//prints "Hindi, 01"

Upvotes: 1

Related Questions