I have a Swift array of tuples, Persons = [(name:String, age:Int)]
.
Now I would like to extract all distinct names in an array of String, name:[String]
from Persons
array. How can I get it.
Asked
Active
Viewed 161 times
1

Sazzad Hissain Khan
- 37,929
- 33
- 189
- 256
-
1Have you tried anything yet? You could map the array to only extract the `"name"` and then put the result in a set. – luk2302 Apr 28 '16 at 09:47
-
I don't know how to use map. I think there might be a direct way to do it. I am just wondering – Sazzad Hissain Khan Apr 28 '16 at 09:49
2 Answers
3
Small code example for you how to use map
function:
struct Person {
let name: String
let age: Int
}
let people = [
Person(name: "Oleg", age: 24),
Person(name: "Igor", age: 26),
]
let names: [String] = people.map { return $0.name }

Oleg Gordiichuk
- 15,240
- 7
- 60
- 100
-
-
-
1This is a nice example for you http://stackoverflow.com/questions/6127638/nsarray-equivalent-of-map – Oleg Gordiichuk Apr 28 '16 at 09:59
-
Actually the OP should use the struct at once rather than the tuple ;-) – vadian Apr 28 '16 at 10:45