1

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.

Sazzad Hissain Khan
  • 37,929
  • 33
  • 189
  • 256

2 Answers2

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
3
let distinctNames = Set(Persons.map { $0.name })
Alexander Doloz
  • 4,078
  • 1
  • 20
  • 36