首先我们必须知道数组的概念:数组是有序的对象集合,一般情况下,一个数组的对象都是相同类型的。数组当中也存在可变数组和不可变数组。
1. 不可变数组 (NSArray)
可变数组 NSMutable 是不可变数组的子类,下面我们先了解一下 NSArray 的用法:
下面通过一个程序例子,来看下它们的具体用法:
1)首先是创建一个数组常用的3种方法:
#import <Foundation/Foundation.h> @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
}
@end int main(int argc,char **argv){
@autoreleasepool {
// NSArray *array1 = [NSArray arrayWithObjects:@"zhangsan",@"lisi",@"wangwu", nil]; 普通的方法 NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu"]; // 常用的简便的方法
for(NSString *temp in array1)
NSLog(@"temp = %@",temp); NSLog(@"-----------------");
// NSArray *array2 = [NSArray arrayWithObjects:@11,@22,@33, nil];
NSArray *array2 = @[@,@,@];
for(NSNumber *temp in array2)
NSLog(@"temp = %li",[temp integerValue]); NSLog(@"-----------------");
Student *stu1 = [[Student alloc] init:@"zhgnsan" andage:];
Student *stu2 = [[Student alloc] init:@"lisi" andage:];
Student *stu3 = [[Student alloc] init:@"wangwu" andage:]; // NSArray *array3 = [NSArray arrayWithObjects:stu1,stu2,stu3, nil];
NSArray *array3 = @[stu1,stu2,stu3];
for(Student *temp in array3)
NSLog(@"name = %@,age = %i",[temp _name],[temp _age]); }
}
2)遍历数组的方法:
#import <Foundation/Foundation.h> @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
}
@end @interface NSString (print){
}
-(void)print;
@end @implementation NSString (print) -(void)print{
NSLog(@"%@",self);
} @end int main(int argc,char **argv){
@autoreleasepool {
NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu"]; //通过新增for遍历
for(NSString *temp in array1)
NSLog(@"temp = %@",temp); NSLog(@"--------------");
NSUInteger count = [array1 count];
for(NSUInteger i = ; i < count; i++)
NSLog(@"array1[%li] = %@",i,array1[i]); NSLog(@"--------------");
for(NSUInteger i = ; i < count; i++)
NSLog(@"array1[%li] = %@",i,[array1 objectAtIndex:i]); NSLog(@"--------------");
[array1 makeObjectsPerformSelector:@selector(print)];//使array中每个成员都执行一次print函数 NSLog(@"------block--------"); //通过函数遍历
[array1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"array1[%li] = %@",idx,obj);
if(idx == )
*stop = YES;
}]; NSLog(@"---------------");
NSEnumerator *enumer = [array1 objectEnumerator]; //通过枚举器进行遍历
NSString *temp = nil;
while(temp = [enumer nextObject]){
NSLog(@"temp = %@",temp);
} NSLog(@"---------------");
NSLog(@"array1 = %@",array1);
NSLog(@"---------------");
NSLog(@"array1 = %@",[array1 description]);
}
}
3)写入和从文件读取的方法
#import <Foundation/Foundation.h> @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
}
@end @interface NSString (print){
}
-(void)print;
@end @implementation NSString (print) -(void)print{
NSLog(@"%@",self);
} @end int main(int argc,char **argv){
@autoreleasepool {
NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu"]; //通过新增for遍历
for(NSString *temp in array1)
NSLog(@"temp = %@",temp); NSString *filename = @"name.plist";
BOOL iswrite = [array1 writeToFile:filename atomically:YES]; //写入文件
if(iswrite)
NSLog(@"write to file ok");
else
NSLog(@"write to file error"); NSArray *array2 = [NSArray arrayWithContentsOfFile:filename]; //从文件读取
[array2 makeObjectsPerformSelector:@selector(print)];
}
}
4)在数组中查找元素
#import <Foundation/Foundation.h> @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
}
@end @interface NSString (print){
}
-(void)print;
@end @implementation NSString (print) -(void)print{
NSLog(@"%@",self);
} @end int main(int argc,char **argv){
@autoreleasepool {
NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu",@"zhangsan"];
NSString *str = [array1 objectAtIndex:]; //查找指定下标的对象
NSString *str2 = array1[];
NSLog(@"str = %@,str2 = %@",str,str2); BOOL ishas = [array1 containsObject:@"wangwu"]; //按指定值查找
if(ishas)
NSLog(@"有");
else
NSLog(@"没有"); NSString *str3 = [array1 firstObject]; //获取array中的第1个数据
NSString *str4 = [array1 lastObject]; //获取array中的最后的数据
NSLog(@"str3 = %@,str4 = %@",str3,str4); NSUInteger index = [array1 indexOfObject:@"zhangsan"]; //按值找下标,第1个
NSLog(@"index = %li",index); NSUInteger index2 = [array1 indexOfObject:@"zhangsan" inRange:NSMakeRange(index + , [array1 count] - )]; //在指定范围按值查找下标
NSLog(@"index2 = %li",index2);
}
}
5)数组的添加和删除,注意这里的添加和删除并不会对原有数组产生影响,因为它是不可变的,修改后会生成一个新的数组。
#import <Foundation/Foundation.h> @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
}
@end @interface NSString (print){
}
-(void)print;
@end @implementation NSString (print) -(void)print{
NSLog(@"%@",self);
} @end int main(int argc,char **argv){
@autoreleasepool {
NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu"];
NSArray *array2 = [array1 arrayByAddingObject:@"zhaoliu"]; //将某个对象加入到数组
NSLog(@"array2 = %@",array2); NSArray *array3 = @[@"aaa",@"bbb",@"ccc"];
NSArray *array4 = [array1 arrayByAddingObjectsFromArray:array3]; //将另一个数组数据加入到一个数组中,组成一个新的数组
NSLog(@"array4 = %@",array4); // array1[0] = @"abc"; //不可变数组不能删除
NSArray *array5 = [array4 subarrayWithRange:NSMakeRange(,)]; //获得array指定范围的子数组
NSLog(@"array5 = %@",array5);
}
}
6)数组的排序
#import <Foundation/Foundation.h> @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
-(NSInteger)cmpstuname:(Student *)stu;
-(NSInteger)cmpstuage:(Student *)stu;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
} -(NSInteger)cmpstuname:(Student *)stu{
return [self._name compare:stu._name];
} -(NSInteger)cmpstuage:(Student *)stu{
return self._age - stu._age;
}
@end int main(int argc,char **argv){
@autoreleasepool {
NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu",@"zhangsan"];
NSLog(@"before sort array1 = %@",array1); NSLog(@"-------------------");
NSArray *array2 = [array1 sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"after sort array2 = %@",array2); NSLog(@"-------------------");
NSArray *array3 = [array1 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
NSLog(@"after sort array3 = %@",array3); NSLog(@"-------------------");
Student *stu1 = [[Student alloc] init:@"zhangsan" andage:];
Student *stu2 = [[Student alloc] init:@"lisi" andage:];
Student *stu3 = [[Student alloc] init:@"wangwu" andage:];
NSArray *stuarray = @[stu1,stu2,stu3];
// NSArray *srotstuarr = [stuarray sortedArrayUsingSelector:@selector(cmpstuname:)]; //按名排序
NSArray *srotstuarr = [stuarray sortedArrayUsingSelector:@selector(cmpstuage:)]; //按年龄排序
for(Student *temp in srotstuarr)
NSLog(@"name = %@,age = %i",temp._name,temp._age); NSLog(@"----------------");
NSArray *sortstuarr2 = [stuarray sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student * obj2) {
return [obj1._name compare:obj2._name];
}];
for(Student *temp in sortstuarr2)
NSLog(@"name = %@,age = %i",temp._name,temp._age); NSLog(@"----------------");
Student *stu4 = [[Student alloc] init:@"zhangsan" andage:];
Student *stu5 = [[Student alloc] init:@"lisi" andage:];
Student *stu6 = [[Student alloc] init:@"wangwu" andage:];
NSArray *stuarray2 = @[stu4,stu5,stu6];
NSArray *allstuarray = [stuarray arrayByAddingObjectsFromArray:stuarray2];
for(Student *temp in allstuarray)
NSLog(@"name = %@,age = %i",temp._name,temp._age); //使用描述符排序
NSSortDescriptor *namescr = [NSSortDescriptor sortDescriptorWithKey:@"_name" ascending:NO]; //第1排序为按名降序排序
NSSortDescriptor *agescr = [NSSortDescriptor sortDescriptorWithKey:@"_age" ascending:YES]; //第2排序为按年龄升序排序
NSArray *sortallstu = [allstuarray sortedArrayUsingDescriptors:@[namescr,agescr]];
NSLog(@"-------------- ");
for(Student *temp in sortallstu)
NSLog(@"name = %@,age = %i",temp._name,temp._age);
}
}
7)分割和连接
#import <Foundation/Foundation.h> @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
} @end int main(int argc,char **argv){
@autoreleasepool {
NSString *path = @"/Users/user/workspace/OCTest/OCTest";
NSArray *patharray = [path componentsSeparatedByString:@"/"]; //将字符串按指定分隔符进行分割,分割形成数组
NSLog(@"patharray = %@",patharray); NSString *str = [patharray componentsJoinedByString:@"-"]; //将数组中的数组用指定的分隔符连接成字符串
NSLog(@"str = %@",str);
}
}
2. 可变字符串 (NSMutable)
因为它是 NSArray 的子类,所以它继承了 NSArray 中的一些方法,所以下面只介绍它的常用的方法构建和修改。首先先看一下它的方法:
下面通过一个例子来具体看它的用法:
1)构造
#import <Foundation/Foundation.h> @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
} @end int main(int argc,char **argv){
@autoreleasepool {
// NSMutableArray *marray = @[@"zhangsan",@"lisi",@"wangwu"]; //错误,类型不一致
NSMutableArray *marray = [NSMutableArray arrayWithCapacity:];
marray.array = @[@"zhangsan",@"lisi",@"wangwu"];
for(NSString *temp in marray)
NSLog(@"temp = %@",temp); NSLog(@"----------------");
NSArray *array = @[@"zhangsan",@"lisi",@"wangwu"];
NSMutableArray *marray2 = [NSMutableArray arrayWithArray:array]; //使用一个不可数组直接构造可变数组
for(NSString *temp in marray2)
NSLog(@"temp = %@",temp); NSLog(@"----------------");
NSMutableArray *marray3 = [[NSMutableArray alloc] initWithArray:array];
for(NSString *temp in marray3)
NSLog(@"temp = %@",temp);
}
}
2)修改
#import <Foundation/Foundation.h> @interface Student : NSObject{
NSString *_name;
int _age;
}
@property (copy,nonatomic)NSString *_name;
@property (assign,nonatomic)int _age;
-(id)init:(NSString *)name andage:(int)age;
@end @implementation Student
@synthesize _name,_age; -(id)init:(NSString *)name andage:(int)age{
self = [super init]; if(self){
self->_name = [name copy];
self->_age = age;
} return self;
} @end int main(int argc,char **argv){
@autoreleasepool {
NSMutableArray *marray = [NSMutableArray array];
[marray addObject:@"zhangsan"]; //将指定对象添加到数组 NSArray *array = @[@"lisi",@"wangwu"];
[marray addObjectsFromArray:array]; //将指定数组添加数组
NSLog(@"marray = %@",marray); NSLog(@"---------------");
[marray insertObject:@"zhaoli" atIndex:]; //向指定下标添加数据
NSLog(@"marray = %@",marray); NSLog(@"---------------");
[marray removeLastObject]; //删除最后一个
NSLog(@"marray = %@",marray); NSLog(@"---------------");
[marray removeObjectAtIndex:]; //删除指定下标
NSLog(@"marray = %@",marray); NSLog(@"---------------");
[marray removeObject:@"zhangsan"]; //按值删除
NSLog(@"marray = %@",marray); NSLog(@"---------------");
[marray addObject:@"aaa"];
[marray addObject:@"bbb"];
[marray removeObjectsInRange:NSMakeRange(, )]; //将指定范围的数据删除
NSLog(@"marray = %@",marray); NSLog(@"---------------");
[marray addObject:@"aaa"];
[marray addObject:@"lisi"];
[marray removeObject:@"lisi" inRange:NSMakeRange(,)]; //从指定范围删除指定对象
NSLog(@"marray = %@",marray); NSLog(@"---------------");
[marray removeAllObjects]; //删除所有数据
NSLog(@"marray = %@",marray); }
}