您的位置:首页 > 移动开发 > IOS开发

iOS 查询数组中的对象 谓词NSPredicate

2016-08-05 14:21 651 查看
1.NSString 对象  
  
NSArray  *array =@["123", @"234" , @"345"];  
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [cd] %@", "2"];  
NSArray *filterdArray = [array filterdArrayUsingPredicate:predicate];  
NSLog(@"%@", filterdArray );  
//output : @"123", "234"  
  
2.含有属性的对象  
  
@interface Person: NSObject   
{  
    NSString *_name;  
    NSString *_telephone;  
    NSInteger _id;  
}  
  
@property (nonatomic, copy) NSString *name;  
@property (nonatomic, copy) NSString *telephone;  
@property (nonatomic, assign) NSInteger id;  
  
@end  
//  
  
1).等于查询  
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", "Ansel"];   
NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate];  
  
2).模糊查询  
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name CONTAINS %@", @"A"]; //predicate只能是对象  
NSArray *filteredArray = [array filteredArrayUsingPredicate:predicate]; 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: