NSMutableArray
//1.for循环遍历:获取数组索引,支持跳跃遍历(类似 i+2 拿到偶数)
NSMutableArray *array = [NSMutableArray array];//拿到数组里面是空的
for (int i = 0; i < 100 ; i ++) {
[array addObject:[NSString stringWithFormat:@"%d",i]];//增加元素
}
//1.for循环遍历:获取数组索引,支持跳跃遍历(类似 i+2 拿到偶数)
for (int i = 0; i < [array count]; i ++) {
id object = [array objectAtIndex:i]; //下标查找
NSLog(@"%@",object); //打印
}
// 2.快速枚举 //百万级别数据 //快速 简单
NSMutableArray *array = [NSMutableArray array];//拿到数组里面是空的
[array addObject:@"100"];
[array addObject:[NSNumber numberWithInt:50]]; //添加元素,不同类型的
//id表示泛型,任意对象
for (id object in array) { //因为数组内部的类型是杂乱的要判断它是什么类型
//判断对象是否为NSString类型
if ([object isKindOfClass:[NSString class]]) { //把类型统一归纳
NSString *string = (NSString *)object;
NSLog(@"NSString value = %@",string);
//判断对象是否为NSString类型
}else if ([object isKindOfClass:[NSNumber class]]){
NSNumber *number = (NSNumber *)object;
NSLog(@"NSNumber value = %@",number);
}
}
//isKindOfClass:判断当前对象类型
NSMutableDictionary
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
for (int i = 0; i < 100; i ++) {
NSString *key = [NSString stringWithFormat:@"key%d", i+1]; //这里的话就要穿两个了,一个键一个值
NSString *value = [NSString stringWithFormat:@"%d", i+1];
[dictionary setObject:value forKey:key];
}
//for循环
//快速枚举
for(NSString *key in [dictionary allKeys]){ //字典是通过键来找到他的位置的那就遍历键就行了
NSString *value = [dictionary objectForKey:key];
NSLog(@"key = %@ value = %@",key ,value);
}