स्विफ्ट 5 समान वस्तुओं की एक सरणी से संपत्ति मूल्यों की एक सरणी प्राप्त करने के कई तरीके प्रदान करता है। अपनी आवश्यकताओं के अनुसार, आप अपनी समस्या को हल करने के लिए प्लेग्राउंड कोड उदाहरणों में से छह का चयन कर सकते हैं ।
1. map
विधि का उपयोग करना
स्विफ्ट के साथ, Sequence
प्रोटोकॉल के अनुरूप प्रकार एक 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. 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]
3. while
लूप का उपयोग करना
ध्यान दें कि स्विफ्ट के साथ, पर्दे के पीछे, एक for
लूप सिर्फ while
एक sequence
'itter ( अधिक विवरण के लिए 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. एक का उपयोग करना और प्रोटोकॉल के struct
अनुरूप हैIteratorProtocol
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]
5. Collection
प्रोटोकॉल विस्तार और का उपयोग करना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]
ध्यान दें कि इस उदाहरण को class Employee
इनहेरिट करने की आवश्यकता है 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]
}
map
करता है - यह की एक सरणी बदल देती हैEmployee
की एक सरणी के लिएInt
, के साथ भराid
क्षेत्र। जो कहने के बराबर है "सभी उदाहरणों से आईडी फ़ील्ड निकालेंEmployee
और उन्हें एक सरणी में डालें"