如何使用NSPredicate返回整个集/数组?

Pra*_*ogg 2 iphone cocoa cocoa-touch

我正在构建一个carArray并希望有条件地过滤内容,使用NSPredicate如下:

NSPredicate *pred;
switch (carType) {
  case FreeCar:
    pred = [NSPredicate predicateWithFormat:@"premium = NO"];
    break;
  case PremiumCar:
    pred = [NSPredicate predicateWithFormat:@"premium = YES"];
    break;
  default:
    pred = [NSPredicate predicateWithFormat:@"SOME PREDICATE THAT RETURNS EVERYTHING"];
    break;
}
self.carArray = [aCarArrayIGotFromSomewhere filteredArrayUsingPredicate:pred];
Run Code Online (Sandbox Code Playgroud)

我的问题是,我存在的值的正确语法是什么,SOME PREDICATE THAT RETURNS EVERYTHING它返回数组/集中的所有实例?

Pra*_*ogg 10

要返回给定数组/集的所有内容:

迅速:

NSPredicate(value: true)

例如:

let array : NSArray = ["a", "b", "c", "d", "e"] let predicate = NSPredicate(value: true) array.filteredArrayUsingPredicate(predicate)

收益率:

["a", "b", "c", "d", "e"]

要排除给定数组/集的所有内容:

迅速:

NSPredicate(value: false)

例如:

let array : NSArray = ["a", "b", "c", "d", "e"] let predicate = NSPredicate(value: false) array.filteredArrayUsingPredicate(predicate)

收益率:

[]

  • 在Swift中,NSPredicate(值:true)`效果很好. (4认同)
  • 而不是[NSPredicate predicateWithFormat:@"TRUEPREDICATE"]你可以做[NSPredicate predicateWithValue:YES] (3认同)
  • 不幸的是,相关的文档对于如何使用这个TRUEPREDICATE东西是相当沉默的.在任何情况下我都无法做到.所以,我搜索了Stack Overflow并且曾经没有更聪明:( (2认同)