Ios 如何过滤多个字典的结构数组
我怎样才能在星期六到星期六 我试过下面的方法。 对这种结构进行排序/过滤的最佳方式是什么Ios 如何过滤多个字典的结构数组,ios,objective-c,Ios,Objective C,我怎样才能在星期六到星期六 我试过下面的方法。 对这种结构进行排序/过滤的最佳方式是什么 NSArray *array = [NSArray arrayWithObject:[NSMutableDictionary dictionaryWithObject:@"filter string" forKey:@"updated_time"]]; // you can also do same for Name key... NSArray *filteredarray = [array
NSArray *array = [NSArray arrayWithObject:[NSMutableDictionary dictionaryWithObject:@"filter string" forKey:@"updated_time"]]; // you can also do same for Name key...
NSArray *filteredarray = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(email == %@)", @"filter string"]];
您需要将日期从
NSString
解析为NSDate
对象,然后使用NSCalendar
提取月份。这将帮助您:
NSArray *array = @[@{@"co_user_id": @25,
@"curr_timpestamp": @"2013-10-09 16:57:39",
@"id": @81,
@"status": @0,
@"story_title": @"Second Story",
@"updated_time": @"2013-10-12 05:32:13",
@"user_id": @24
},
@{@"co_user_id": @25,
@"curr_timpestamp": @"2013-10-09 16:57:39",
@"id": @82,
@"status": @0,
@"story_title": @"Second Story",
@"updated_time": @"2013-10-12 05:32:13",
@"user_id": @24
}];
// Create a basic date formatter to turn the strings into dates.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// Create the calendar for retrieving date components.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Set the month being filtered
NSInteger month = 10;
// Filter for dictionaries whose 'updated_time's have the same value as 'month'.
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
NSDate *date = [formatter dateFromString:evaluatedObject[@"updated_time"]];
return [[calendar components:NSMonthCalendarUnit fromDate:date] month] == month;
}];
// Print the filtered array.
NSLog(@"%@", [array filteredArrayUsingPredicate:predicate]);
尝试:
这充分利用了您的时间格式非常严格的优势。过滤器是否位于
curr\u timestamp
或updated\u time
中的月份值上?@DavidAtkinson-其on updated\u time如果不是字典中的键,为什么通过电子邮件进行过滤?
NSArray *array = @[@{@"co_user_id": @25,
@"curr_timpestamp": @"2013-10-09 16:57:39",
@"id": @81,
@"status": @0,
@"story_title": @"Second Story",
@"updated_time": @"2013-10-12 05:32:13",
@"user_id": @24
},
@{@"co_user_id": @25,
@"curr_timpestamp": @"2013-10-09 16:57:39",
@"id": @82,
@"status": @0,
@"story_title": @"Second Story",
@"updated_time": @"2013-10-12 05:32:13",
@"user_id": @24
}];
// Create a basic date formatter to turn the strings into dates.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// Create the calendar for retrieving date components.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Set the month being filtered
NSInteger month = 10;
// Filter for dictionaries whose 'updated_time's have the same value as 'month'.
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
NSDate *date = [formatter dateFromString:evaluatedObject[@"updated_time"]];
return [[calendar components:NSMonthCalendarUnit fromDate:date] month] == month;
}];
// Print the filtered array.
NSLog(@"%@", [array filteredArrayUsingPredicate:predicate]);
NSPredicate* predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"updated_time CONTAINS '-%02d-'", 10]];
NSArray* result = [data filteredArrayUsingPredicate:predicate];