简体   繁体   English

NSPredicate针对NSArray中的嵌套值

[英]NSPredicate against nested values in NSArray

I have an NSArray of SKNodes, and am trying to use an NSPredicate to sort on the location.y values of the SKNodes using a predicate filter. 我有一个SKNodes的NSArray,并且正在尝试使用NSPredicate来使用谓词过滤器对SKNodes的location.y值进行排序。 I am trying the following, but at runtime I am getting the 'this class is not key value coding-compliant for the key y' So, I assume I have to do something else to filter on a subcompnent of the object? 我正在尝试以下操作,但是在运行时我得到的提示是:“此类不符合键y的键值编码标准”,因此,我认为我还必须执行其他操作来对对象的子组件进行过滤? IS this possible? 这可能吗?

NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"position.y"
                                                             ascending:YES];
NSArray *results = [myarrayofsknodes sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];

The comment by LearnCocos2D is correct. LearnCocos2D的评论是正确的。 However, you can sort other ways. 但是,您可以使用其他方式进行排序。

NSArray *original; // Populate this elsewhere
NSArray *results = [original sortedArrayUsingComparator:^(MyObject *obj1, MyObject *obj2) {
    if (obj1.position.y==obj2.position.y) {
        return NSOrderedSame;
    } else if (obj1.position.y < obj2.position.y) {
        return NSOrderedAscending;
    } else {
        return NSOrderedDescending;
    }
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM