There's a class called Employee
.
有一个叫Employee的类。
class Employee {
var id: Int
var firstName: String
var lastName: String
var dateOfBirth: NSDate?
init(id: Int, firstName: String, lastName: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
}
}
And I have an array of Employee
objects. What I now need is to extract the id
s of all those objects in that array into a new array.
我有一个Employee对象数组。我现在需要的是将数组中所有这些对象的id提取到一个新的数组中。
I also found this similar question. But it's in Objective-C so it's using valueForKeyPath
to accomplish this.
我也发现了类似的问题。但它在Objective-C中,所以它使用valueForKeyPath来完成这个。
How can I do this in Swift?
我怎么能在Swift中做到这一点呢?
2 个解决方案
#1
136
You can use the map
method, which transform an array of a certain type to an array of another type - in your case, from array of Employee
to array of Int
:
您可以使用map方法,它将一个特定类型的数组转换为另一个类型的数组——在您的例子中,从雇员数组转换为Int数组:
var array = [Employee]()
array.append(Employee(id: 4, firstName: "", lastName: ""))
array.append(Employee(id: 2, firstName: "", lastName: ""))
let ids = array.map { $0.id }
#2
45
Swift 3 offers many ways to get an array of property values from an array of similar objects. According to your needs and tastes, you may choose one of the six following Playground code examples to solve your problem.
Swift 3提供了从相似对象数组中获取属性值数组的多种方法。根据您的需要和品味,您可以选择以下六种游戏中的一种来解决您的问题。
1. Using map
method
Swift provides a map(_:)
method for types that conform to Sequence
protocol (including Array
). (see also Transforming an Array)
Swift为符合序列协议(包括数组)的类型提供一个map(_:)方法。(参见转换数组)
class Employee {
let id: Int, firstName: String, lastName: String
init(id: Int, firstName: String, lastName: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
}
}
let employeeArray = [
Employee(id: 1, firstName: "Jon", lastName: "Skeet"),
Employee(id: 2, firstName: "Darin", lastName: "Dimitrov"),
Employee(id: 4, firstName: "Hans", lastName: "Passant")
]
let idArray = employeeArray.map({ (employee: Employee) -> Int in
employee.id
})
// let idArray = employeeArray.map { $0.id } // also works
print(idArray) // prints [1, 2, 4]
2. Using for
loop
class Employee {
let id: Int, firstName: String, lastName: String
init(id: Int, firstName: String, lastName: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
}
}
let employeeArray = [
Employee(id: 1, firstName: "Jon", lastName: "Skeet"),
Employee(id: 2, firstName: "Darin", lastName: "Dimitrov"),
Employee(id: 4, firstName: "Hans", lastName: "Passant")
]
var idArray = [Int]()
for employee in employeeArray {
idArray.append(employee.id)
}
print(idArray) // prints [1, 2, 4]
3. Using while
loop
Note that with Swift, behind the scenes, a for
loop is just a while
loop over a sequence
's iterator (see IteratorProtocol for more details).
注意,对于Swift,在幕后,for循环只是对序列的迭代器的一个while循环(有关更多细节,请参阅IteratorProtocol)。
class Employee {
let id: Int, firstName: String, lastName: String
init(id: Int, firstName: String, lastName: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
}
}
let employeeArray = [
Employee(id: 1, firstName: "Jon", lastName: "Skeet"),
Employee(id: 2, firstName: "Darin", lastName: "Dimitrov"),
Employee(id: 4, firstName: "Hans", lastName: "Passant")
]
var idArray = [Int]()
var iterator = employeeArray.makeIterator()
while let employee = iterator.next() {
idArray.append(employee.id)
}
print(idArray) // prints [1, 2, 4]
4. Using a struct
that conforms to IteratorProtocol
and Sequence
protocols
class Employee {
let id: Int, firstName: String, lastName: String
init(id: Int, firstName: String, lastName: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
}
}
struct EmployeeSequence: Sequence, IteratorProtocol {
let employeeArray: [Employee]
private var index = 0
init(employeeArray: [Employee]) {
self.employeeArray = employeeArray
}
mutating func next() -> Int? {
guard index < employeeArray.count else { return nil }
defer { index += 1 }
return employeeArray[index].id
}
}
let employeeArray = [
Employee(id: 1, firstName: "Jon", lastName: "Skeet"),
Employee(id: 2, firstName: "Darin", lastName: "Dimitrov"),
Employee(id: 4, firstName: "Hans", lastName: "Passant")
]
let employeeSequence = EmployeeSequence(employeeArray: employeeArray)
let idArray = Array(employeeSequence)
print(idArray) // prints [1, 2, 4]
5. Using Collection
protocol extension and AnyIterator
class Employee {
let id: Int, firstName: String, lastName: String
init(id: Int, firstName: String, lastName: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
}
}
extension Collection where Iterator.Element: Employee {
func getIDs() -> Array<Int> {
var index = startIndex
let iterator: AnyIterator<Int> = AnyIterator {
defer { index = self.index(index, offsetBy: 1) }
return index != self.endIndex ? self[index].id : nil
}
return Array(iterator)
}
}
let employeeArray = [
Employee(id: 1, firstName: "Jon", lastName: "Skeet"),
Employee(id: 2, firstName: "Darin", lastName: "Dimitrov"),
Employee(id: 4, firstName: "Hans", lastName: "Passant")
]
let idArray = employeeArray.getIDs()
print(idArray) // prints [1, 2, 4]
6. Using KVC and NSArray
's value(forKeyPath:)
method
Note that this example requires class Employee
to inherit from NSObject
.
注意,这个示例要求类Employee从NSObject继承。
import Foundation
class Employee: NSObject {
let id: Int, firstName: String, lastName: String
init(id: Int, firstName: String, lastName: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
}
}
let employeeArray = [
Employee(id: 1, firstName: "Jon", lastName: "Skeet"),
Employee(id: 2, firstName: "Darin", lastName: "Dimitrov"),
Employee(id: 4, firstName: "Hans", lastName: "Passant")
]
let employeeNSArray = employeeArray as NSArray
if let idArray = employeeNSArray.value(forKeyPath: #keyPath(Employee.id)) as? [Int] {
print(idArray) // prints [1, 2, 4]
}
See GitHub's Iterating-over-arrays repo for more examples.
有关更多示例,请参见GitHub的遍历数组repo。
#1
136
You can use the map
method, which transform an array of a certain type to an array of another type - in your case, from array of Employee
to array of Int
:
您可以使用map方法,它将一个特定类型的数组转换为另一个类型的数组——在您的例子中,从雇员数组转换为Int数组:
var array = [Employee]()
array.append(Employee(id: 4, firstName: "", lastName: ""))
array.append(Employee(id: 2, firstName: "", lastName: ""))
let ids = array.map { $0.id }
#2
45
Swift 3 offers many ways to get an array of property values from an array of similar objects. According to your needs and tastes, you may choose one of the six following Playground code examples to solve your problem.
Swift 3提供了从相似对象数组中获取属性值数组的多种方法。根据您的需要和品味,您可以选择以下六种游戏中的一种来解决您的问题。
1. Using map
method
Swift provides a map(_:)
method for types that conform to Sequence
protocol (including Array
). (see also Transforming an Array)
Swift为符合序列协议(包括数组)的类型提供一个map(_:)方法。(参见转换数组)
class Employee {
let id: Int, firstName: String, lastName: String
init(id: Int, firstName: String, lastName: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
}
}
let employeeArray = [
Employee(id: 1, firstName: "Jon", lastName: "Skeet"),
Employee(id: 2, firstName: "Darin", lastName: "Dimitrov"),
Employee(id: 4, firstName: "Hans", lastName: "Passant")
]
let idArray = employeeArray.map({ (employee: Employee) -> Int in
employee.id
})
// let idArray = employeeArray.map { $0.id } // also works
print(idArray) // prints [1, 2, 4]
2. Using for
loop
class Employee {
let id: Int, firstName: String, lastName: String
init(id: Int, firstName: String, lastName: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
}
}
let employeeArray = [
Employee(id: 1, firstName: "Jon", lastName: "Skeet"),
Employee(id: 2, firstName: "Darin", lastName: "Dimitrov"),
Employee(id: 4, firstName: "Hans", lastName: "Passant")
]
var idArray = [Int]()
for employee in employeeArray {
idArray.append(employee.id)
}
print(idArray) // prints [1, 2, 4]
3. Using while
loop
Note that with Swift, behind the scenes, a for
loop is just a while
loop over a sequence
's iterator (see IteratorProtocol for more details).
注意,对于Swift,在幕后,for循环只是对序列的迭代器的一个while循环(有关更多细节,请参阅IteratorProtocol)。
class Employee {
let id: Int, firstName: String, lastName: String
init(id: Int, firstName: String, lastName: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
}
}
let employeeArray = [
Employee(id: 1, firstName: "Jon", lastName: "Skeet"),
Employee(id: 2, firstName: "Darin", lastName: "Dimitrov"),
Employee(id: 4, firstName: "Hans", lastName: "Passant")
]
var idArray = [Int]()
var iterator = employeeArray.makeIterator()
while let employee = iterator.next() {
idArray.append(employee.id)
}
print(idArray) // prints [1, 2, 4]
4. Using a struct
that conforms to IteratorProtocol
and Sequence
protocols
class Employee {
let id: Int, firstName: String, lastName: String
init(id: Int, firstName: String, lastName: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
}
}
struct EmployeeSequence: Sequence, IteratorProtocol {
let employeeArray: [Employee]
private var index = 0
init(employeeArray: [Employee]) {
self.employeeArray = employeeArray
}
mutating func next() -> Int? {
guard index < employeeArray.count else { return nil }
defer { index += 1 }
return employeeArray[index].id
}
}
let employeeArray = [
Employee(id: 1, firstName: "Jon", lastName: "Skeet"),
Employee(id: 2, firstName: "Darin", lastName: "Dimitrov"),
Employee(id: 4, firstName: "Hans", lastName: "Passant")
]
let employeeSequence = EmployeeSequence(employeeArray: employeeArray)
let idArray = Array(employeeSequence)
print(idArray) // prints [1, 2, 4]
5. Using Collection
protocol extension and AnyIterator
class Employee {
let id: Int, firstName: String, lastName: String
init(id: Int, firstName: String, lastName: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
}
}
extension Collection where Iterator.Element: Employee {
func getIDs() -> Array<Int> {
var index = startIndex
let iterator: AnyIterator<Int> = AnyIterator {
defer { index = self.index(index, offsetBy: 1) }
return index != self.endIndex ? self[index].id : nil
}
return Array(iterator)
}
}
let employeeArray = [
Employee(id: 1, firstName: "Jon", lastName: "Skeet"),
Employee(id: 2, firstName: "Darin", lastName: "Dimitrov"),
Employee(id: 4, firstName: "Hans", lastName: "Passant")
]
let idArray = employeeArray.getIDs()
print(idArray) // prints [1, 2, 4]
6. Using KVC and NSArray
's value(forKeyPath:)
method
Note that this example requires class Employee
to inherit from NSObject
.
注意,这个示例要求类Employee从NSObject继承。
import Foundation
class Employee: NSObject {
let id: Int, firstName: String, lastName: String
init(id: Int, firstName: String, lastName: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
}
}
let employeeArray = [
Employee(id: 1, firstName: "Jon", lastName: "Skeet"),
Employee(id: 2, firstName: "Darin", lastName: "Dimitrov"),
Employee(id: 4, firstName: "Hans", lastName: "Passant")
]
let employeeNSArray = employeeArray as NSArray
if let idArray = employeeNSArray.value(forKeyPath: #keyPath(Employee.id)) as? [Int] {
print(idArray) // prints [1, 2, 4]
}
See GitHub's Iterating-over-arrays repo for more examples.
有关更多示例,请参见GitHub的遍历数组repo。