ios - 过滤对象的 NSArray 以具有唯一的属性对象

标签 ios objective-c nspredicate

我有 Manager 对象和 Account 对象。 Manager 对象有一个属性 Account。

有一个 Manager 对象的 NSArray,它需要被 Account 唯一!

示例数组:

    NSArray *managers = @[Manager1(has Account1), Manager2(has Account1), Manager3(has Account1), Manager4(has Acconut2), Manager5(has Account2)];

我需要一个只有 Manager1 和 Manager4 的 NSArray。

最佳答案

你需要的是 distinctUnionOfObjects

[managers valueForKeyPath:@"@distinctUnionOfObjects.Account"];

这是documentation

@distinctUnionOfObjects The @distinctUnionOfObjects operator returns an array containing the distinct objects in the property specified by the key path to the right of the operator.



以下示例返回删除了任何重复值的交易中交易的收款人属性值:
NSArray *payees = [transactions valueForKeyPath:@"@distinctUnionOfObjects.payee"];

生成的收款人数组包含以下字符串:Car Loan、General Cable、Animal Hospital、Green Power、Mortgage。

@unionOfObjects 操作符类似,但不会删除重复的对象。

关于ios - 过滤对象的 NSArray 以具有唯一的属性对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28921240/

相关文章:

ios - 使用 AVAssetWriterVideoInput 无法将帧附加到视频文件。最后失败并出现未记录的错误 -12738

objective-c - 按字母顺序OBJ-C识别字符

ios - UIPickerView - 移除曲率并添加文本

ios - 如何避免此处搜索栏和表格 View 之间的额外间隙并在它们之间放置分隔符?

ios - 复数 NSLocalizedString 中的字符串参数

ios - iOS模拟器中的UITextView输入光标问题

iphone - 以编程方式实现 UIScrollView

ios - 如何使用 predicateWithFormat 查询 NSString 包含在 NSSet 中

ios - NSPredicate 没有返回预期的项目

objective-c - "This class is not key value > coding-compliant for the key previewB"但我已经删除了预览B