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

标签 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];

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

最佳答案

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

swift :

NSPredicate(值:true)

例如:

let array : NSArray = ["a", "b", "c", "d", "e"] 让谓词 = NSPredicate(值: true) array.filteredArrayUsingPredicate(谓词)

产量:

[“a”、“b”、“c”、“d”、“e”]

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

swift :

NSPredicate(值: false)

例如:

let array : NSArray = ["a", "b", "c", "d", "e"] 让谓词 = NSPredicate(值: false) array.filteredArrayUsingPredicate(谓词)

产量:

[]

关于iphone - 如何使用 NSPredicate 返回整个集合/数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2535693/

相关文章:

iphone - 在 iPhone 中定义属性的最佳方式是什么

iphone - 如何限制平移手势区域?

ios - UIAlertController 位置错误

cocoa-touch - 如何从目录中删除特定文件夹

iphone - 从 mapView 检索当前用户位置,而 map 不在屏幕上

iphone - UITableViewCell : how to give the built-in imageView a margin on retina display

swift - 获取在 ViewController 类中启动应用程序的文件名?

objective-c - 创建目录AtPath : doesn't work

objective-c - Objective C 发送不兼容的 block 指针类型

ios - Swift:如何重写 scrollView 的方法?