[示例]NSDictionary-数组中增加字典,并遍历数组
代码:
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { NSDictionary *dic1=@{@"name":@"yang", @"class":@"IOS6", @"age":@"24"}; NSDictionary *dic2=@{@"name":@"ming", @"class":@"IOS8", @"age":@"27"}; NSMutableArray *arr1=[NSMutableArray array]; //初始化数组 [arr1 addObject:dic1]; //将字典增加位数组元素 [arr1 addObject:dic2]; for(int i=0;i<arr1.count;i++) { //下标法遍历数组 NSDictionary *dic=arr1[i]; NSLog(@"班级:%@,姓名:%@,年龄:%@",dic[@"name"],dic[@"class"],dic[@"age"]); //dic[@"xxx"]是[dic objectForKey:xxx]的简写形式 } } return 0; }