如何在我的代码中添加循环

时间:2016-12-08 05:04:15

标签: ios objective-c for-loop nsarray nsdate

我有NSMutableArray *arrList,我在MutableArray *arrList创建了 Monday 对象。但是现在是静态写入条件,看起来像我的代码:(if ([str containsString:@"Monday"]))。我想要一个静态 Monday 的数组加载位置。这意味着在静态星期一逐个循环加载。

NSArray*loopAry = [[NSArray alloc]initWithObjects:@"Sunday",@"Monday",@"Tuesday",nil];

这是loopAry个对象重新加载以放置静态星期一

这有助于获取动态代码并仅在循环中重新加载数组并获取工作日数据

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"]) {
        [result addObject:element];
    }
}

// print result
NSLog(@"%@",result);
 (
"28-11-2016, Monday",
"05-12-2016, Monday"
)

1 个答案:

答案 0 :(得分:1)

您可以获得如下所示的提供日期数组:

NSArray *loopAry = [[NSArray alloc]initWithObjects:@"Sunday",@"Monday",@"Tuesday",nil];

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 (NSString *strData in  arrList){
    //2nd Loop for your weekdays array.By this you will get weekdays array in result.
    for (NSString *strDay in loopAry) {
        if ([strData containsString:strDay]) {
              [result addObject:strData];
        }
    }

}

// print result
NSLog(@"%@",result);

希望这能帮助你度过工作日。