Il y a une classe appelée 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
}
}
Et j'ai un tableau d'objets Employee
. Il me faut maintenant extraire les id
s de tous les objets de ce tableau dans un nouveau tableau.
J'ai aussi trouvé ce même question . Mais il est en Objective-C et utilise donc valueForKeyPath
pour accomplir cela.
Comment puis-je faire cela dans Swift?
Vous pouvez utiliser la méthode map
, qui transforme un tableau d'un certain type en un tableau d'un autre type - dans votre cas, d'un tableau de Employee
à un tableau de Int
:
var array = [Employee]()
array.append(Employee(id: 4, firstName: "", lastName: ""))
array.append(Employee(id: 2, firstName: "", lastName: ""))
let ids = array.map { $0.id }
Swift 5 offre de nombreuses façons d'obtenir un tableau de valeurs de propriétés à partir d'un tableau d'objets similaires. En fonction de vos besoins, vous pouvez choisir l’un des six exemples de code Playground suivants pour résoudre votre problème.
map
Avec Swift, les types conformes au protocole Sequence
ont une méthode map(_:)
. L'exemple de code suivant montre comment l'utiliser:
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]
for
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]
while
Notez qu'avec Swift, dans les coulisses, une boucle for
n'est qu'une boucle while
sur un itérateur de sequence
(voir IteratorProtocol pour plus de détails).
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]
struct
conforme aux protocoles IteratorProtocol
et Sequence
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]
Collection
extension de protocole et 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]
NSArray
value(forKeyPath:)
Notez que cet exemple nécessite que class Employee
hérite de NSObject
.
import Foundation
class Employee: NSObject {
@objc 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]
}