开发者

How do I return the entire set / array using NSPredicate?

开发者 https://www.devze.com 2022-12-25 12:01 出处:网络
I\'m building a carArray and want to filter the contents conditionally, using an NSPredicate, like so:

I'm building a carArray and want to filter the contents conditionally, using an NSPredicate, like so:

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 = [开发者_如何学JAVAaCarArrayIGotFromSomewhere filteredArrayUsingPredicate:pred];

My question is, what is the correct syntax for the value I've stubbed in as SOME PREDICATE THAT RETURNS EVERYTHING which returns all of the instances in the array / set?


To return all of the contents a given array / set:

Swift:

NSPredicate(value: true)

for example:

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

yields:

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

To exclude all of the contents a given array / set:

Swift:

NSPredicate(value: false)

for example:

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

yields:

[]


For always true or always false you can use [NSPredicate predicateWithValue:YES] or [NSPredicate predicateWithValue:NO].

0

精彩评论

暂无评论...
验证码 换一张
取 消