如何数组遍历完成在执行相应方法 ios
1个回答
展开全部
//普通的for循环遍历
-(void)iteratorWithFor
{
//////////处理数组//////////
NSArray *arrayM = @[@"1",@"2",@"3",@"4"];
NSInteger arrayMCount = [arrayM count];
for (int i = 0; i<arrayMCount; i++) {
NSString *obj = arrayM[i];
NSLog(@"%@",obj);
}
//////////处理字典//////////
NSDictionary *dictM = @{@"1":@"one",@"2":@"two",@"3":@"three"};
NSArray *dictKeysArray = [dictM allKeys];
for (int i = 0; i<dictKeysArray.count; i++) {
NSString *key = dictKeysArray[i];
NSString *obj = [dictM objectForKey:key];
NSLog(@"%@:%@",key,obj);
}
//////////处理集合//////////
NSSet * setM = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", nil];
NSArray *setObjArray = [setM allObjects];
for (int i = 0; i<setObjArray.count; i++) {
NSString *obj = setObjArray[i];
NSLog(@"%@",obj);
}
//////////反向遍历----降序遍历----以数组为例
NSArray *arrayM2 = @[@"1",@"2",@"3",@"4"];
NSInteger arrayMCount2 = [arrayM2 count] - 1;
for (NSInteger i = arrayMCount2; i>0; i--) {
NSString *obj = arrayM2[i];
NSLog(@"%@",obj);
}
}
-(void)iteratorWithFor
{
//////////处理数组//////////
NSArray *arrayM = @[@"1",@"2",@"3",@"4"];
NSInteger arrayMCount = [arrayM count];
for (int i = 0; i<arrayMCount; i++) {
NSString *obj = arrayM[i];
NSLog(@"%@",obj);
}
//////////处理字典//////////
NSDictionary *dictM = @{@"1":@"one",@"2":@"two",@"3":@"three"};
NSArray *dictKeysArray = [dictM allKeys];
for (int i = 0; i<dictKeysArray.count; i++) {
NSString *key = dictKeysArray[i];
NSString *obj = [dictM objectForKey:key];
NSLog(@"%@:%@",key,obj);
}
//////////处理集合//////////
NSSet * setM = [[NSSet alloc] initWithObjects:@"one",@"two",@"three",@"four", nil];
NSArray *setObjArray = [setM allObjects];
for (int i = 0; i<setObjArray.count; i++) {
NSString *obj = setObjArray[i];
NSLog(@"%@",obj);
}
//////////反向遍历----降序遍历----以数组为例
NSArray *arrayM2 = @[@"1",@"2",@"3",@"4"];
NSInteger arrayMCount2 = [arrayM2 count] - 1;
for (NSInteger i = arrayMCount2; i>0; i--) {
NSString *obj = arrayM2[i];
NSLog(@"%@",obj);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询