Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 670 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get an array of property values from an object array

#1
There's a class called `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.

I also found this similar [question][1]. But it's in Objective-C so it's using `valueForKeyPath` to accomplish this.

How can I do this in Swift?


[1]:

[To see links please register here]

Reply

#2
Swift 5 offers many ways to get an array of property values from an array of similar objects. According to your needs, you may choose one of the **six following Playground code examples** to solve your problem.

---

## 1. Using `map` method

With Swift, types that conform to `Sequence` protocol have a [`map(_:)`][1] method. The following sample code shows how to use it:

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][3] for more details).

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:)`][4] method

Note that this example requires `class Employee` to inherit from `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]
}


[1]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

Reply

#3
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`:

var array = [Employee]()
array.append(Employee(id: 4, firstName: "", lastName: ""))
array.append(Employee(id: 2, firstName: "", lastName: ""))

let ids = array.map { $0.id }

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through