Creating an array from properties of objects in another array

后端 未结 1 1111
心在旅途
心在旅途 2021-01-30 01:45

Is there any convenient way to take an array/set of objects and create a new array/set containing some property of each item in the first array?

For example, an array co

相关标签:
1条回答
  • 2021-01-30 02:16

    This will return an array containing the value of licensePlate from each item in the myCars array:

    NSArray *licensePlates = [myCars valueForKeyPath:@"licensePlate"]
    

    If you want only unique items (for example), you can do something like this:

    NSArray *licensePlates = [myCars valueForKeyPath:@"@distinctUnionOfObjects.licensePlate"];
    

    For more possibilities, see the Collection Operators documentation in the Key-Value Coding Programming Guide.

    0 讨论(0)
提交回复
热议问题