NSArray的基本应用总结
NSString *string1 = @"two";
NSString *string2 = @"one";
//创建数组
NSArray *array1 = [NSArray arrayWithObjects:string1,string2, nil];
NSArray *array2 = [NSArray arrayWithArray:array1];
//创建数组只包含已有数组的一部分
NSRange range = NSMakeRange(0, 2);
NSArray *subArray = [array1 subarrayWithRange:range];
//array的长度
int arrayLength = [array1 count];
//访问数组中特定位置的一个对象
NSString *string = [array1 objectAtIndex:0];
//是否包含指定对象
BOOL isInArray = [array1 containsObject:string1];
//对象在数组中的位置
int index = [array1 indexOfObject:string1];
//遍历一个数组中的值
for(NSString *obj in array1)
{
NSLog(@"%@",obj);
}
//反向遍历一个数组的值
for(NSString *objfan in [array1 reverseObjectEnumerator])
{
NSLog(@"%@",objfan);
}
//对字符串的重新排序(有点问题)
//[array1 sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
//NSLog(@"array1 = %@",array1);
//创建动态数组
NSMutableArray *myarray = [[NSMutableArray alloc]initWithObjects:string1,string2, nil];
NSString *string3 = @"Three";
//向动态数组中添加
[myarray addObject:string3];
[myarray addObject:string2];
[myarray insertObject:string3 atIndex:0];
[myarray insertObject:string2 atIndex:1];
//替换
[myarray replaceObjectAtIndex:1withObject:string3];
//删除
[myarray removeObject:string3];
//删除特定位置对象
[myarray removeObjectAtIndex:0];
//删除几个对象
[myarray removeObjectsInRange:range];
//删除所有对象
[myarray removeAllObjects];