NSPredicate: The NSPredicate class is used to define logical conditions for fetch data/object. NSPredicate filter the objects and we can create the predicates in formatted string which is parsed by class methods of NSPredicate.
We can make an condition with different operation such as basic comparisons(=,==,>=,<=,>,<,etc),basic compound predicates(AND,&&,OR,||,NOT,!),string comparisons,Aggregate operations,etc
Ref.: Apple Doc. - http://developer.apple.com/library/mac/#documentation/cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html
Example: Comparison of Integer value:
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"3",nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF >= '3'"];
[arr filterUsingPredicate:predicate];
NSLog(@"%@",[arr description]);
Comparison of String value:
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@"A",@"B",@"C",@"AB",@"AC",@"CB",nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH 'C'"];
[arr filterUsingPredicate:predicate];
NSLog(@"%@",[arr description]);
Comparison of attributes value:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"job == 'Programmer'"];
[listOfItems filterUsingPredicate:predicate];
NSLog(@"%@",[listOfItems description]);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"salary >= 10000"];
[listOfItems filterUsingPredicate:predicate];
NSLog(@"%@",[listOfItems description]);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'T'"];
[listOfItems filterUsingPredicate:predicate];
NSLog(@"%@",[listOfItems description]);
thanks
keep :->ing
We can make an condition with different operation such as basic comparisons(=,==,>=,<=,>,<,etc),basic compound predicates(AND,&&,OR,||,NOT,!),string comparisons,Aggregate operations,etc
Ref.: Apple Doc. - http://developer.apple.com/library/mac/#documentation/cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html
Example: Comparison of Integer value:
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"3",nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF >= '3'"];
[arr filterUsingPredicate:predicate];
NSLog(@"%@",[arr description]);
Comparison of String value:
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@"A",@"B",@"C",@"AB",@"AC",@"CB",nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH 'C'"];
[arr filterUsingPredicate:predicate];
NSLog(@"%@",[arr description]);
Comparison of attributes value:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"job == 'Programmer'"];
[listOfItems filterUsingPredicate:predicate];
NSLog(@"%@",[listOfItems description]);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"salary >= 10000"];
[listOfItems filterUsingPredicate:predicate];
NSLog(@"%@",[listOfItems description]);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'T'"];
[listOfItems filterUsingPredicate:predicate];
NSLog(@"%@",[listOfItems description]);
thanks
keep :->ing
Thank you, that saved a lot of time. I was getting " Unable to parse message ".
ReplyDelete