1 数组基本
1.1 创建数组
NSArray 是不可变数组,一旦创建完成就不能够对数组进行,添加,删除等操作
下面这种方式相当于什么都没做
NSArray * array = [[NSArray alloc] init];
NSLog(@"%@",array);
1.2 通过构造方法的方式创建一个NSArray
在创建一个NSArray的时候,集合的最后一个元素一定是nil
NSArray * array1 = [NSArray arrayWithObjects:@"one",@"two",@"three", nil];
NSLog(@"%@",array1);
1.3 数组中可以存储不同类型的对象
NSNumber * number = [NSNumber numberWithInt:10];
NSArray * array2 = [[NSArray alloc] initWithObjects:@"one",@"two",number, nil];
NSLog(@"array2 %@",array2);
1.4 数组实际上存储的是对象的地址,同样也可以存储数组的地址
NSArray * a1 = [[NSArray alloc] initWithObjects:@"one",@"two",@"three", nil];
NSArray * a2 = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];
NSArray * a3 = [[NSArray alloc] initWithObjects:a1,a2, nil];
NSLog(@"a3 %@",a3);
1.5 存储自定义的对象
最好都 description 方法
@interface Person : NSObject
@property (nonatomic,strong)NSString * name;
@property (nonatomic,assign)int age;
- (id)initWithName:(NSString *)name andAge:(int)age;
@end
@implementation Person
- (id)initWithName:(NSString *)name andAge:(int)age
{
if (self = [super init])
{
_name = name;
_age = age;
}
return self;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"age = %d name = %@",_age,_name];
}
@end
Person * p1 = [[Person alloc] initWithName:@"xiaozhe" andAge:20];
Person * p2 = [[Person alloc] initWithName:@"hell" andAge:18];
Person * p3 = [[Person alloc] initWithName:@"marray" andAge:38];
NSArray * array3 = [[NSArray alloc] initWithObjects:p1,p2,p3, nil];
NSLog(@"array3 %@",array3);
1.6 数组中存储基本数据类型
如果你要在数组中存储基本数据类型,请包装好了之后在去存 NSNumber
注意不要把nil值存储到 NSArray 中,会导致数据丢失
NSString * str = nil;
NSArray * array4 = [[NSArray alloc] initWithObjects:@"2",str,[NSNumber numberWithInt:23], nil];
NSLog(@"array4 %@",array4);
1.7 创建数组的快捷方式
NSArray * karray = @[@"a",@"b",@"c"];
NSLog(@"karray %@",karray);
1.8 快速获得一个数组中的元素
NSString * kstr = karray[0];
NSLog(@"kstr %@",kstr);
1.9 从数组中取出数据
NSArray * array = [[NSArray alloc] initWithObjects:@"one",@"two",@"three", nil];
数组的index时从0开始的
NSString * str = [array objectAtIndex:0];
NSLog(@"str %@",str);
1.10 获得数组的元素个数
NSUInteger arrayCount = [array count];
NSLog(@"arrayCount %d",arrayCount);
1.11 判断数组中是否存在某个对象
Person * p1 = [[Person alloc] initWithName:@"yofer" andAge:20];
Person * p2 = [[Person alloc] initWithName:@"luke" andAge:30];
NSArray * array = @[p1,p2];
BOOL isContain = [array containsObject:p1];
if (isContain)
{
NSLog(@"存在");
}else
{
NSLog(@"不存在");
}
2 遍历数组
2.1 使用for循环遍历数组
NSArray * array = @[@"one",@"two",@"three"];
for (int i = 0; i < array.count; i++)
{
NSString * str = array[i];
NSLog(@"array[%d] = %@",i,str);
}
2.2 使用增强for循环的方式遍历数组
for (NSString * str in array)
{
NSLog(@"str = %@",str);
}
2.3 注意事项
如果你的数组中村粗了多种不同类型的数据,那么最好不要调用某个对象特有的方法,会导致程序崩溃
在实际的开发中,一个数组往往只负责存储一种数据类型
Person * p1 = [[Person alloc] init];
NSArray * array2 = @[@"one",[NSNumber numberWithInt:10],p1];
for (NSString * str in array2)
{
// NSLog(@"array2 str %@",str);
// NSLog(@"str %ld",str.length);
}
2.4 枚举器
NSArray * array3 = @[@"one",@"two",@"three"];
NSEnumerator * enumerateor = [array3 objectEnumerator];
NSString * value;
while (value = [enumerateor nextObject]) {
NSLog(@"enum str %@",value);
}
3 数组排序
3.1 使用 sortedArrayUsingSelector
是最简单的排序方式
数组是按照你存入元素的顺序存储的
NSArray * array = @[@"b",@"d",@"a",@"z"];
NSLog(@"排序前 array %@",array);
array = [array sortedArrayUsingSelector:@selector(compare:)];
// NSArray * array1 = [array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"排序后 array %@",array);
3.2 使用block方式排序
NSArray * array2 = @[@"z",@"4",@"b",@"3",@"x"];
NSLog(@"array2 排序前 %@",array2);
array2 = [array2 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
/*
NSComparisonResult retuslt = [obj1 compare:obj2];
return retuslt;
*/
//obj1 compare obj2 就是正序排序
// return [obj1 compare:obj2];
//obj2 compare obj1 就是倒序排序
return [obj2 compare:obj1];
}];
NSLog(@"array2 排序后 %@",array2);
先给一个 Person 类
@interface Person : NSObject
@property (nonatomic,assign) int age;
@property (nonatomic,strong) NSString * name;
@property (nonatomic,strong) NSString * year;
- (id)initWithName:(NSString *)name andAge:(int)age andYear:(NSString *)year;
//+ (id)personWithName:(NSString *)name andAge:(int)age andYear:(NSString *)year;
@end
@implementation Person
- (id)initWithName:(NSString *)name andAge:(int)age andYear:(NSString *)year
{
if (self = [super init])
{
_name = name;
_age = age;
_year = year;
}
return self;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"name %@ age %d year %@",_name,_age,_year];
}
@end
3.3 定制化数组对象排序
Person * p1 = [[Person alloc] initWithName:@"xiaozhe" andAge:20 andYear:@"1990"];
Person * p2 = [[Person alloc] initWithName:@"alex" andAge:18 andYear:@"2990"];
Person * p3 = [[Person alloc] initWithName:@"merry" andAge:25 andYear:@"1890"];
NSArray * array3 = @[p1,p2,p3];
NSLog(@"array3 排序前 %@",array3);
如果你向给你自己定义的对象排序,必须根据某一个属性来排序,
sortDescriptorWithKey 参数要的就是你对象中,要依据哪个属性来排序,你就把哪个属性的名字当成key传入
ascending YES表示正序 NO表示倒序
NSSortDescriptor * d1 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:NO];
NSSortDescriptor * d2 = [NSSortDescriptor sortDescriptorWithKey:@"year" ascending:NO];
如果你要使用多个属性进行排序,默认在前面的NSSortDescriptor优先级比较高
NSArray * descripts = @[d2,d1];
array3 = [array3 sortedArrayUsingDescriptors:descripts];
NSLog(@"array 3 排序后 %@",array3);
结果:
2016-08-14 14:13:57.238 05-数组排序[2105:516991] array3 排序前 (
"name xiaozhe age 20 year 1990",
"name alex age 18 year 2990",
"name merry age 25 year 1890"
)
2016-08-14 14:13:57.238 05-数组排序[2105:516991] array 3 排序后 (
"name alex age 18 year 2990",
"name xiaozhe age 20 year 1990",
"name merry age 25 year 1890"
)
再次测试:
NSArray * array4 = @[p1,p2,p3];
NSLog(@"array4 排序前 %@",array4);
array4 = [array4 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
Person * p1 = obj1;
Person * p2 = obj2;
//year
return [p1.year compare:p2.year];
}];
NSLog(@"array4 排序后 %@",array4);
结果:
2016-08-14 14:13:57.238 05-数组排序[2105:516991] array4 排序前 (
"name xiaozhe age 20 year 1990",
"name alex age 18 year 2990",
"name merry age 25 year 1890"
)
2016-08-14 14:13:57.238 05-数组排序[2105:516991] array4 排序后 (
"name merry age 25 year 1890",
"name xiaozhe age 20 year 1990",
"name alex age 18 year 2990"
)
4 可变数组: NSMutableArray
NSMutableArray 继承至 NSArray
4.1 创建可变数组
NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity:0];
使用addObject 动态给数组中增加元素
[array addObject:@"one"];
[array addObject:@"two"];
[array addObject:@"three"];
[array addObject:@"one"];
NSString * str1 = @"one";
NSString * str2 = @"two";
NSString * str3 = @"three";
数组中可以存储,同一个对象多次
[array addObject:str1];
[array addObject:str2];
[array addObject:str3];
[array addObject:str1];
4.2 指定对象插入的位置
[array insertObject:str1 atIndex:2];
4.3 删除 会通过对象,删除数组中所有的同一个地址的对象
[array removeObject:str1];
4.4 通过索引的方式删除对象,超出了数组的count值,那么就会导致异常 index beyond bounds
[array removeObjectAtIndex:0];
[array addObject:str2];
[array addObject:str3];
[array addObject:str1];
4.5 删除数组中所有的元素
[array removeAllObjects];
NSLog(@"array %@",array);
[array addObject:str2];
[array addObject:str3];
[array addObject:str1];
5 可变数组 NSMutableArray 遍历
5.1 for换遍历
for (int i = 0; i < array.count; i++)
{
NSString * str = [array objectAtIndex:i];
//在遍历数组的时候,千万不要给数组中,增加,或者删除元素
// [array removeObject:str1];
NSLog(@"str %@",str);
}
5.2 增强for循环
for (NSString * str in array)
{
// [array removeObject:str1];
//如果你在增强for循环中,删除元素,会直接导致异常出现
NSLog(@"str %@",str);
}
5.3 枚举器
NSEnumerator * enumerator = [array objectEnumerator];
NSString * value;
while (value = [enumerator nextObject]) {
//如果你在枚举器中,删除元素,会直接导致异常出现
// [array removeObject:str1];
NSLog(@"str %@",value);
}
5.4 要通过遍历的方式确定删除哪个元素怎么办
NSMutableArray * array2 = [[NSMutableArray alloc] init];
[array2 addObject:@"1"];
[array2 addObject:@"2"];
[array2 addObject:@"3"];
[array2 addObject:@"4"];
[array2 addObject:@"5"];
NSMutableArray * tmp = [[NSMutableArray alloc] init];
for (NSString * str in array2)
{
if ([str isEqualToString:@"3"])
{
[tmp addObject:str];
}
}
NSLog(@"array2 %@",array2);
NSLog(@"tmp %@",tmp);
结果:
2016-08-14 14:35:20.437 06-可变数组[2252:538261] array2 (
1,
2,
3,
4,
5
)
2016-08-14 14:35:20.437 06-可变数组[2252:538261] tmp (
3
)
遍历临时数组
for (int i = 0; i < tmp.count; i++)
{
NSString * str = [tmp objectAtIndex:i];
//从原数组中删除,临时数组中存储的对象
[array2 removeObject:str];
}
NSLog(@"array2 %@",array2);
作者:zyq522376829 发表于2016/8/14 15:08:09 原文链接
阅读:30 评论:0 查看评论