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
  • 33,857
  • 26
  • 164
  • 227

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
  • 14,647
  • 5
  • 57
  • 94
3
let distinctNames = Set(Persons.map { $0.name })
Alexander Doloz
  • 3,740
  • 1
  • 18
  • 33