#pragma mark ------------for循环遍历集合中的元素------
//创建一个数组,包含5个字符串对象,倒序取出数组中的所有元素,并存储到另一可变数组中
NSArray *array = @[@"1", @"2", @"3", @"4", @"5"];
NSMutableArray *marray = [NSMutableArray arrayWithCapacity:0];
for (NSInteger i = [array count] - 1; i >= 0 ; i --) {
[marray addObject:array[i]];
}
NSLog(@"marray is %@", marray);
NSDictionary *dic = @{@"name":@"zhonger",@"sex":@"m", @"age":@"29"};
NSArray *key = [dic allKeys];
NSArray *value = [dic allValues];
for (int i = 0; i < [key count]; i ++) {
//NSLog(@"%@ = %@",key[i], value[i]);
//NSLog(@"%@", [dic objectForKey:key[i]]);
NSLog(@"%@",dic[key[i]]);
//数组访问元素或者对象的方法是? objectAtIndex:
//数组访问元素的语法糖? 数组对象[下标]
//字典访问value值的方法是? object ForKey:
//字典访问值的语法糖? 字典对象[key]
}
//创建一个集体,保存三条信息,遍历得到每一条信息
NSSet *set = [NSSet setWithObjects:@"1", @"888888", @"3", nil];
NSArray *allSet = [set allObjects];
for (int i = 0; i < [allSet count]; i ++) {
NSLog(@"allSet is %@",allSet[i]);
}
#pragma mark ------------枚举器 NSEnumerator------
NSArray *enumArray = @[@"jinkangda", @"zhubada", @"dazi", @"bada"];
//创建一个枚举器,依附于数组
NSEnumerator *rator = [enumArray objectEnumerator]; //正序
//创建一个对象
id object = nil; //容量为空
while (object = [rator nextObject]) {
NSLog(@"正序==%@",object);
}
NSEnumerator *rator1 = [enumArray reverseObjectEnumerator];
id object1 = nil;
while (object1 = [rator1 nextObject]) {
NSLog(@"倒序==%@",object1);
}
//字典枚举遍历
NSDictionary *enumDic =
@{@"key1":@{@"key1":@"value1"},
@"key2":@{@"key2":@"value2"},
@"key3":@{@"key3":@"value3"}};
NSEnumerator *dicRator = [enumDic objectEnumerator];
id value1 = nil;
while (value1 = [dicRator nextObject]) {
NSLog(@"value1 is %@",value1);
}
#pragma mark ------------for...in..快速枚举------
//type *object 代表从集合中获取的对象类型 object只是一个名称而已,只需要满足变量命名法则就行
//collection 代表遍历的集合,要在哪个集合中遍历
//statements 遍历中需要执行的操作,比如使用枚举出来的对象实现某些功能
/*
for (<#type *object#> in <#collection#>) {
<#statements#>
}*/
for (id object in enumArray) {
NSLog(@"-----%@",object);
}
//使用forin遍历获取字典中的每个value
//在字典中遍历key,通过得到每一个key值得到每一个value
for (id key in enumDic) {
//NSLog(@"%@",[enumDic objectForKey:key]);
NSLog(@"%@",enumDic[key]);
for (id key1 in enumDic[key]) {
NSLog(@"%@",enumDic[key][key1]);
// NSLog(@"%@",[[enumDic objectForKey:key]objectForKey:key1]);
}
}
//定义⼀一个数组, 包含5个字符串对象, 取出数组中所有字 符串, 并拼接⽣生成⼀一个新的字符串。
NSArray *ar = @[@"I", @"L", @"o", @"v", @"eyou"];
NSMutableString *mstr = [[NSMutableString alloc]initWithCapacity:0];
for (id object in ar) {
//[mstr stringByAppendingString:object];
[mstr appendString:object];
}
NSLog(@"%@",mstr);