我有以下格式的一系列词典
[ { "student": { "id": "1", "studentUserDetail": { "firstName": "Bonny", "lastName": "Roby" } } }, { "student": { "id": "1", "studentUserDetail": { "firstName": "Bonny", "lastName": "Kety" } } }, { "student": { "id": "1", "studentUserDetail": { "firstName": "Arther", "lastName": "Fone" } } }, ]
在上面的数组中,我需要在内部键student.StudentUserDetails.firstName中过滤包含serachKey(例如Bonny)的所有元素.我如何使用NSPredicate进行过滤
NSString *name = @"Bonny"; NSPredicate *pred = [NSPredicate predicateWithFormat: @"student.studentUserDetail.firstName == %@", name]; NSArray *arr = [self.anArray filteredArrayUsingPredicate:pred]; NSLog(@"Bonny found at : %@", arr);
编辑:
如果要基于模式进行搜索,请使用:
NSPredicate *pred = [NSPredicate predicateWithFormat: @"student.studentUserDetail.firstName beginswith[cd] %@", name];