如何获取 NSMutableArray 特定对象
How to get NSMutableArray specific objects
我想获取星期日和星期一文本对象下方的数组列表并将其保存在另一个数组中。换句话说,获取具有日期和星期名称的对象都被获取并保存在数组
中
请帮助我该怎么做。我很努力,但没有得到正确答案。
NSDateComponents *components;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy"];
NSDate *startDate = [dateFormat dateFromString:@"25-11-2016"];
NSDate *endDate = [dateFormat dateFromString:@"7-12-2016"];
[dateFormat setDateFormat:@"dd-MM-yyyy, EEEE"];
components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:startDate toDate:endDate options:0];
int days = [components day];
for (int x = 0; x <= days; x++) {
[mutable_ary addObject:[dateFormat stringFromDate:startDate]];
NSLog(@"%@",[dateFormat stringFromDate:startDate]);
startDate = [startDate dateByAddingTimeInterval:(60 * 60 * 24)];
}
NSLog(@"%@",mutable_ary);
{
"25-11-2016, Friday",
"26-11-2016, Saturday",
"27-11-2016, Sunday",
"28-11-2016, Monday",
"29-11-2016, Tuesday",
"30-11-2016, Wednesday",
"01-12-2016, Thursday",
"02-12-2016, Friday",
"03-12-2016, Saturday",
"04-12-2016, Sunday",
"05-12-2016, Monday",
"06-12-2016, Tuesday",
"07-12-2016, Wednesday"
}
输出:
{
"27-11-2016, Sunday",
"28-11-2016, Monday",
"04-12-2016, Sunday",
"05-12-2016, Monday",
}
NSArray *input = @[@"25-11-2016, Friday",
@"26-11-2016, Saturday",
@"27-11-2016, Sunday",
@"28-11-2016, Monday",
@"29-11-2016, Tuesday",
@"30-11-2016, Wednesday",
@"01-12-2016, Thursday",
@"02-12-2016, Friday",
@"03-12-2016, Saturday",
@"04-12-2016, Sunday",
@"05-12-2016, Monday",
@"06-12-2016, Tuesday",
@"07-12-2016, Wednesday"];
NSMutableArray *days = [NSMutableArray arrayWithArray: @[@"Sunday",@"Monday"]];
NSMutableArray *output = [NSMutableArray new];
for(NSString *day in days){
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains %@",day];
NSArray *filtered = [input filteredArrayUsingPredicate:predicate];
[output addObjectsFromArray:filtered];
}
NSLog(@"%@",output);
试试这个:
NSMutableArray *arrList = [[NSMutableArray alloc]initWithObjects:@"25-11-2016, Friday",
@"26-11-2016, Saturday",
@"27-11-2016, Sunday",
@"28-11-2016, Monday",
@"29-11-2016, Tuesday",
@"30-11-2016, Wednesday",
@"01-12-2016, Thursday",
@"02-12-2016, Friday",
@"03-12-2016, Saturday",
@"04-12-2016, Sunday",
@"05-12-2016, Monday",
@"06-12-2016, Tuesday",
@"07-12-2016, Wednesday", nil];
NSMutableArray *result = [[NSMutableArray alloc]init];
for (id element in arrList){
NSString *str = element;
if ([str containsString:@"Monday"] || [str containsString:@"Sunday"]){
[result addObject:element];
}
}
// print result
NSLog(@"%@",result);
我想获取星期日和星期一文本对象下方的数组列表并将其保存在另一个数组中。换句话说,获取具有日期和星期名称的对象都被获取并保存在数组
中请帮助我该怎么做。我很努力,但没有得到正确答案。
NSDateComponents *components;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy"];
NSDate *startDate = [dateFormat dateFromString:@"25-11-2016"];
NSDate *endDate = [dateFormat dateFromString:@"7-12-2016"];
[dateFormat setDateFormat:@"dd-MM-yyyy, EEEE"];
components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:startDate toDate:endDate options:0];
int days = [components day];
for (int x = 0; x <= days; x++) {
[mutable_ary addObject:[dateFormat stringFromDate:startDate]];
NSLog(@"%@",[dateFormat stringFromDate:startDate]);
startDate = [startDate dateByAddingTimeInterval:(60 * 60 * 24)];
}
NSLog(@"%@",mutable_ary);
{
"25-11-2016, Friday",
"26-11-2016, Saturday",
"27-11-2016, Sunday",
"28-11-2016, Monday",
"29-11-2016, Tuesday",
"30-11-2016, Wednesday",
"01-12-2016, Thursday",
"02-12-2016, Friday",
"03-12-2016, Saturday",
"04-12-2016, Sunday",
"05-12-2016, Monday",
"06-12-2016, Tuesday",
"07-12-2016, Wednesday"
}
输出:
{
"27-11-2016, Sunday",
"28-11-2016, Monday",
"04-12-2016, Sunday",
"05-12-2016, Monday",
}
NSArray *input = @[@"25-11-2016, Friday",
@"26-11-2016, Saturday",
@"27-11-2016, Sunday",
@"28-11-2016, Monday",
@"29-11-2016, Tuesday",
@"30-11-2016, Wednesday",
@"01-12-2016, Thursday",
@"02-12-2016, Friday",
@"03-12-2016, Saturday",
@"04-12-2016, Sunday",
@"05-12-2016, Monday",
@"06-12-2016, Tuesday",
@"07-12-2016, Wednesday"];
NSMutableArray *days = [NSMutableArray arrayWithArray: @[@"Sunday",@"Monday"]];
NSMutableArray *output = [NSMutableArray new];
for(NSString *day in days){
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains %@",day];
NSArray *filtered = [input filteredArrayUsingPredicate:predicate];
[output addObjectsFromArray:filtered];
}
NSLog(@"%@",output);
试试这个:
NSMutableArray *arrList = [[NSMutableArray alloc]initWithObjects:@"25-11-2016, Friday",
@"26-11-2016, Saturday",
@"27-11-2016, Sunday",
@"28-11-2016, Monday",
@"29-11-2016, Tuesday",
@"30-11-2016, Wednesday",
@"01-12-2016, Thursday",
@"02-12-2016, Friday",
@"03-12-2016, Saturday",
@"04-12-2016, Sunday",
@"05-12-2016, Monday",
@"06-12-2016, Tuesday",
@"07-12-2016, Wednesday", nil];
NSMutableArray *result = [[NSMutableArray alloc]init];
for (id element in arrList){
NSString *str = element;
if ([str containsString:@"Monday"] || [str containsString:@"Sunday"]){
[result addObject:element];
}
}
// print result
NSLog(@"%@",result);