objective-c中的NSArray和NSMutableArray
oc中NSArray和NSMutableArray 的常用用法:
View Code
1 //声明一个 NSMutableArray可变数组,数组长度是10; 2 NSMutableArray *myArray=[NSMutableArray arrayWithCapacity:10]; 3 4 5 //动态增加数组内容addObject向可变数组尾部添加数据 6 [myArray addObject:@"1hihi"]; 7 [myArray addObject:@"2hihi"]; 8 [myArray addObject:@"3hihi"]; 9 [myArray addObject:@"4hihi"]; 10 [myArray addObject:@"5hihi"]; 11 [myArray addObject:@"6hihi"]; 12 13 14 NSString *abc=@"我是通过NSSting加进来的哦!"; 15 [myArray addObject:abc]; 16 17 18 //insertObject向数组中的某个位置加入一条数据从0开始 19 [myArray insertObject:@"我是通过insert加进来的哦!" atIndex:1]; 20 21 22 //输出索引是3的数据 23 NSString *SY3=[myArray objectAtIndex:3]; 24 NSLog(@"sy3:%@",SY3); 25 26 27 //移除数组中索引为3的元素 28 [myArray removeObjectAtIndex:3]; 29 30 31 //尝试从index-1获取数据,失败???? 32 //NSString *SY=[myArray objectAtIndex:]; 33 //NSLog(@"sy3:%@",SY); 34 35 //循环输出数组利用NSObject * id in myArray可以较快速的输出 36 for (id object in myArray) 37 { 38 NSLog(@"数组:%@",object); 39 }