OC - NSArray 和 NSNumber

1.数组的创建
NSArray *array1 =[[NSArray alloc]initWithObjects:@"iphone",@"ipad",@"iWatch", nil];
NSArray *array2= [NSArray arrayWithObjects:@"A",@"B",@"C", nil];
NSArray *array3 =@[@"beijin",@"guangzhou",@"shanghai"];(字面量方法创建)
2.通过下标取值
1. array3[1]
2. [array3 objectAtIndex:2]
注意:下标是从0开始
3.添加元素
NSArray  *array4 =[array3 arrayByAddingObject:@"shenzhen"];
4.数组转换为字符串
NSString *str =[array4 componentsJoinedByString:@","];
5.将字符串转化为数组
NSString *str1 =@"ios Andriod WP";
NSArray  *array5 =[str1 componentsSeparatedByString:@" "];
6.通过对象去查找下标
NSInteger index=[array4 indexOfObject:@"shanghai"];
6.通过对象去查找下标
NSInteger index=[array4 indexOfObject:@"shanghai"];
7.数组的便利
for (int i=0; i<[array4 count]; i++){
      id a=[array4 objectAtIndex:i];//因为不知道a是啥类型  所以用id
 }
8.快速便利
for (id b in array4)//不知道啥类型的b,对array4便利
{
     NSLog(@"b=%@",b);
}
9.可变数组
NSMutableArray *mArray =[[NSMutableArray alloc]initWithObjects:@"a",@"b",@"c",@"d", nil];
9.1可变数组追加
[mArray addObject:@"e"];
9.2可变数组插入
[mArray insertObject:@"f" atIndex:2];
9.3可变数组删除
[mArray removeObjectAtIndex:3];
9.4可变数组替换
[mArray replaceObjectAtIndex:2 withObject:@"F"];
10.NSNumber
float  a=3.2;
NSNumber *aNum =[[NSNumber alloc]initWithFloat:a];
NSLog(@"aNum=3.2");
10.1NSNumber将对象类型转化为基本类型
float b=[aNum floatValue];
NSLog(@"b=%.1f",b);b=3.2

NSString *_str=[NSString stringWithFormat:@"123.56"];
int c=[_str  intValue];
NSLog(@"c=%d",c);c=123

你可能感兴趣的:(OC - NSArray 和 NSNumber)