NSarray 常用语法
// 数组赋值的时候 以 nil 结束
//在数组里面不能存放nil的对象
//
NSArray *array1 = [NSArray arrayWithObjects:@"122" ,@"acv",@"dyh", nil];
NSArray *array = @[@"www.",@"xin",@"dian",@".com"];
NSInteger a = array.count;
NSLog(@"a is %ld",a);
// 获取某一个元素
NSString *p = [array objectAtIndex:2];
// 打印值 是 dian
NSLog(@" p is %@",p);
// 判断数组是否包含某一个对象
if ([array containsObject:@"www."]) {
NSLog(@"array is %@",array);
}
// 获取这个元素的下标 索引值
NSInteger b = [array indexOfObject:@"www."];
NSLog(@"b is %ld",b );
//用“.”将数组串成字符串
NSString *c = [array componentsJoinedByString:@"."];
NSLog(@"c is %@",c);
//数组末尾追加字符串
NSArray *d = [array arrayByAddingObject:@"dyh"];
NSLog(@"d is %@",d);
//在一个数组后面依次添加另一个数组
NSArray *array3 = [array arrayByAddingObjectsFromArray:array1];
NSLog(@" array3 = %@",array3);
//可变数组 nsMutableArray
NSMutableArray *mArray = [NSMutableArray arrayWithCapacity:3];
// 添加数据
[mArray addObject:@"dyh"];
[mArray addObject:@"21"];
[mArray addObject:@"yibin"];
//插入元素在索引值为2的元素的前面
[mArray insertObject:@"man" atIndex:2];
NSLog(@"mArray = %@",mArray);
// 删除最后一个元素
// [mArray removeLastObject];
// 删除全部元素
// [mArray removeAllObjects];
// 删除索引值为1的元素
// [mArray removeObjectAtIndex:1];
// 用一个对象替换另一个对象
[mArray replaceObjectAtIndex:2 withObject:@"KoBe"];
NSLog(@"mArray = %@",mArray);
}
return 0;
}