[NSArray]
一 定义
1 不可变数组
2 oc中数组的元素可以是任何对象
3 数字中装有元素的地址
二 初始化
NSArray *变量 = [[NSArry alloc] initWithObjects:对象...,nil];
三 使用场景
1 遍历数组
A 枚举
NSEnumerator *enums = [array1 objectEnumerator];
while (obj=[enums nextObject])
B 下标
for (i=0;i<count;i++) {
C 快速枚举法
for(id obj in array1)
2 获取长度
int count = [数组对象 count];
[NSMutableArray]
一 定义
1 可变数组
2 可以插入任意对象
二 初始化
NSMutableArray *变量名 = [[NSMutableArray alloc] init];
三 使用场景
1 增加对象
[mutableArray addObject:@"JACK"];
2 下标替换对象
[mutableArray exchangeObjectAtIndex:0 withObjectAtIndex:2];
3 移除对象
[mutableArray removeObject:person]; [mutableArray removeObjectAtIndex:0];
4 顺序枚举遍历
NSEnumerator *_enums = [mutableArray objectEnumerator];
5 倒序枚举遍历
NSEnumerator *_reverseEnums = [mutableArray reverSEObjectEnumerator];
6 字符串和数组的转换
NSArray *_strArray = [str componentsSeparatedByString:@" "];
NSString *str = [_strMutableArray componentsJoinedByString:@" "];
////main.m//OC-数组////Created by wangtouwang on 15/3/20.//Copyright (c) 2015年 wangtouwang. All rights reserved.//#import<Foundation/Foundation.h>#import"Person.h"intmain(intargc,constchar*argv[]) {
@autoreleasepool {//不可变数组 NSArryNSLog(@"NSArry");
Person*person = [Personnew];
person._name=@"JACK";//初始化NSArray *array1 = [[NSArray alloc] initWithObjects:@"jack",@"lucy",person, nil];
NSLog(@"%@",array1);//枚举NSLog(@"枚举器法打印数组结果:");
NSEnumerator*enums =[array1 objectEnumerator];idobj;while(obj=[enums nextObject]) {
NSLog(@"%@",obj);
}//快速枚举NSLog(@"快速枚举法打印数组结果:");for(idobjinarray1){
NSLog(@"%@",obj);
}//下标便利NSInteger i =2;idobj2 =[array1 objectAtIndex:i];
NSLog(@"%@",obj2);//获取长度NSInteger count=[array1 count];
NSLog(@"数组长度 %lu",count);
i=0;for(i=0;i<count;i++) {
NSLog(@"%@",[array1 objectAtIndex:i]);
}//可变数组 NSMutableArrayNSMutableArray*mutableArray =[[NSMutableArray alloc] init];//增加[mutableArray addObject:@"JACK"];
[mutableArray addObject:@"MOBILE"];
[mutableArray addObject:@"TEL"];
[mutableArray addObject:@"BOOK"];
[mutableArray addObject:@"STUDENT"];
[mutableArray addObject:person];
NSLog(@"%@",mutableArray);//替换对象NSLog(@"替换对象");
[mutableArray exchangeObjectAtIndex:0withObjectAtIndex:2];
NSLog(@"%@",mutableArray);//移除对象 参数 id类型NSLog(@"移除对象 参数 id类型");
[mutableArray removeObject:person];
NSLog(@"%@",mutableArray);//移除对象 参数 NSIntegerNSLog(@"移除对象 参数 NSInteger");
[mutableArray removeObjectAtIndex:0];
NSLog(@"%@",mutableArray);//正序枚举遍历NSLog(@"正序枚举遍历");
NSEnumerator*_enums =[mutableArray objectEnumerator];idobj3;while(obj3=[_enums nextObject]) {
NSLog(@"%@",obj3);
}//倒序枚举遍历 并且可以遍历时删除NSLog(@"倒序枚举遍历 并且可以遍历时删除");
NSEnumerator*_reverseEnums =[mutableArray reverseObjectEnumerator];while(obj3=[_reverseEnums nextObject]) {
NSLog(@"%@",obj3);
[mutableArray removeObject:obj3];
}
NSLog(@"倒序枚举遍历结束 查看数组长度 %lu",[mutableArray count]);//字符串和数组的转换NSString *str =@"hello world hao are you";
NSArray*_strArray = [str componentsSeparatedByString:@""];
NSLog(@"lenght = %lu",[_strArray count]);
NSMutableArray*_strMutableArray =[[NSMutableArray alloc] init];
NSEnumerator*_strEnums =[_strArray reverseObjectEnumerator];
NSString*objstring ;while(objstring=[_strEnums nextObject]) {
[_strMutableArray addObject:objstring];
}
NSLog(@"%@",_strMutableArray);
str= [_strMutableArray componentsJoinedByString:@""];
NSLog(@"%@",str);
}return0;
}