如何获取NSMutableArray特定对象

时间:2016-12-07 11:53:08

标签: ios objective-c nsmutablearray nsarray nsdate

我希望这个数组列表在星期日和星期一文本对象获取并保存在另一个数组中。换句话说,获取日期和工作日名称的对象都是get并保存在数组中

请帮助我该怎么做。我很努力,但没有得到正确的答案。

   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",
 }

2 个答案:

答案 0 :(得分:2)

试试这个:

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);

答案 1 :(得分:0)

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);