map 返回的数组顺序
Order of array returned by map
虽然 "I know how it works but don't know what it's supposed to be moment" 中的一个突然出现在我的脑海中。假设我有一个数组:
struct Person {
var name : String
var age : Int
}
var people = [Person]()
people.append(Person(name: "John", age: 24))
people.append(Person(name: "Mike", age: 21))
people.append(Person(name: "Emma", age: 23))
我想将 people
映射到两个具有姓名和年龄的不同数组:
let names = people.map { [=11=].name }
let ages = people.map { [=11=].age }
问题:
是否保证结果按原数组的顺序,即names = ["John", "Mike", "Emma"]
和ages = [24, 21, 23]
?
如果第一个问题的答案是否定的,是否能保证names
和ages
同步?
"After applying the provided closure to each array element, the map(_:) method returns a new array containing all of the new mapped values, in the same order as their corresponding values in the original array."
虽然 "I know how it works but don't know what it's supposed to be moment" 中的一个突然出现在我的脑海中。假设我有一个数组:
struct Person {
var name : String
var age : Int
}
var people = [Person]()
people.append(Person(name: "John", age: 24))
people.append(Person(name: "Mike", age: 21))
people.append(Person(name: "Emma", age: 23))
我想将 people
映射到两个具有姓名和年龄的不同数组:
let names = people.map { [=11=].name }
let ages = people.map { [=11=].age }
问题:
是否保证结果按原数组的顺序,即
names = ["John", "Mike", "Emma"]
和ages = [24, 21, 23]
?如果第一个问题的答案是否定的,是否能保证
names
和ages
同步?
"After applying the provided closure to each array element, the map(_:) method returns a new array containing all of the new mapped values, in the same order as their corresponding values in the original array."