個人筆記與總結。
首先我們必須知道數組的概念:數組是有序的對象集合,一般情況下,一個數組的對象都是相同類型的。數組當中也存在可變數組和不可變數組。
1. 不可變數組 (NSArray)
可變數組 NSMutable 是不可變數組的子類,下麵我們先瞭解一下 NSArray 的用法:
下麵通過一個程式例子,來看下它們的具體用法:
1)首先是創建一個數組常用的3種方法:
1 #import <Foundation/Foundation.h> 2 3 @interface Student : NSObject{ 4 NSString *_name; 5 int _age; 6 } 7 @property (copy,nonatomic)NSString *_name; 8 @property (assign,nonatomic)int _age; 9 -(id)init:(NSString *)name andage:(int)age; 10 @end 11 12 @implementation Student 13 @synthesize _name,_age; 14 15 -(id)init:(NSString *)name andage:(int)age{ 16 self = [super init]; 17 18 if(self){ 19 self->_name = [name copy]; 20 self->_age = age; 21 } 22 23 return self; 24 } 25 @end 26 27 int main(int argc,char **argv){ 28 @autoreleasepool { 29 // NSArray *array1 = [NSArray arrayWithObjects:@"zhangsan",@"lisi",@"wangwu", nil]; 普通的方法 30 31 NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu"]; // 常用的簡便的方法 32 for(NSString *temp in array1) 33 NSLog(@"temp = %@",temp); 34 35 NSLog(@"-----------------"); 36 // NSArray *array2 = [NSArray arrayWithObjects:@11,@22,@33, nil]; 37 NSArray *array2 = @[@11,@22,@33]; 38 for(NSNumber *temp in array2) 39 NSLog(@"temp = %li",[temp integerValue]); 40 41 NSLog(@"-----------------"); 42 Student *stu1 = [[Student alloc] init:@"zhgnsan" andage:11]; 43 Student *stu2 = [[Student alloc] init:@"lisi" andage:22]; 44 Student *stu3 = [[Student alloc] init:@"wangwu" andage:33]; 45 46 // NSArray *array3 = [NSArray arrayWithObjects:stu1,stu2,stu3, nil]; 47 NSArray *array3 = @[stu1,stu2,stu3]; 48 for(Student *temp in array3) 49 NSLog(@"name = %@,age = %i",[temp _name],[temp _age]); 50 51 } 52 }
2)遍曆數組的方法:
1 #import <Foundation/Foundation.h> 2 3 @interface Student : NSObject{ 4 NSString *_name; 5 int _age; 6 } 7 @property (copy,nonatomic)NSString *_name; 8 @property (assign,nonatomic)int _age; 9 -(id)init:(NSString *)name andage:(int)age; 10 @end 11 12 @implementation Student 13 @synthesize _name,_age; 14 15 -(id)init:(NSString *)name andage:(int)age{ 16 self = [super init]; 17 18 if(self){ 19 self->_name = [name copy]; 20 self->_age = age; 21 } 22 23 return self; 24 } 25 @end 26 27 @interface NSString (print){ 28 } 29 -(void)print; 30 @end 31 32 @implementation NSString (print) 33 34 -(void)print{ 35 NSLog(@"%@",self); 36 } 37 38 @end 39 40 int main(int argc,char **argv){ 41 @autoreleasepool { 42 NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu"]; //通過新增for遍歷 43 for(NSString *temp in array1) 44 NSLog(@"temp = %@",temp); 45 46 NSLog(@"--------------"); 47 NSUInteger count = [array1 count]; 48 for(NSUInteger i = 0; i < count; i++) 49 NSLog(@"array1[%li] = %@",i,array1[i]); 50 51 NSLog(@"--------------"); 52 for(NSUInteger i = 0; i < count; i++) 53 NSLog(@"array1[%li] = %@",i,[array1 objectAtIndex:i]); 54 55 NSLog(@"--------------"); 56 [array1 makeObjectsPerformSelector:@selector(print)];//使array中每個成員都執行一次print函數 57 58 NSLog(@"------block--------"); //通過函數遍歷 59 [array1 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 60 NSLog(@"array1[%li] = %@",idx,obj); 61 if(idx == 1) 62 *stop = YES; 63 }]; 64 65 NSLog(@"---------------"); 66 NSEnumerator *enumer = [array1 objectEnumerator]; //通過枚舉器進行遍歷 67 NSString *temp = nil; 68 while(temp = [enumer nextObject]){ 69 NSLog(@"temp = %@",temp); 70 } 71 72 NSLog(@"---------------"); 73 NSLog(@"array1 = %@",array1); 74 NSLog(@"---------------"); 75 NSLog(@"array1 = %@",[array1 description]); 76 } 77 }
3)寫入和從文件讀取的方法
1 #import <Foundation/Foundation.h> 2 3 @interface Student : NSObject{ 4 NSString *_name; 5 int _age; 6 } 7 @property (copy,nonatomic)NSString *_name; 8 @property (assign,nonatomic)int _age; 9 -(id)init:(NSString *)name andage:(int)age; 10 @end 11 12 @implementation Student 13 @synthesize _name,_age; 14 15 -(id)init:(NSString *)name andage:(int)age{ 16 self = [super init]; 17 18 if(self){ 19 self->_name = [name copy]; 20 self->_age = age; 21 } 22 23 return self; 24 } 25 @end 26 27 @interface NSString (print){ 28 } 29 -(void)print; 30 @end 31 32 @implementation NSString (print) 33 34 -(void)print{ 35 NSLog(@"%@",self); 36 } 37 38 @end 39 40 int main(int argc,char **argv){ 41 @autoreleasepool { 42 NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu"]; //通過新增for遍歷 43 for(NSString *temp in array1) 44 NSLog(@"temp = %@",temp); 45 46 NSString *filename = @"name.plist"; 47 BOOL iswrite = [array1 writeToFile:filename atomically:YES]; //寫入文件 48 if(iswrite) 49 NSLog(@"write to file ok"); 50 else 51 NSLog(@"write to file error"); 52 53 NSArray *array2 = [NSArray arrayWithContentsOfFile:filename]; //從文件讀取 54 [array2 makeObjectsPerformSelector:@selector(print)]; 55 } 56 }
4)在數組中查找元素
1 #import <Foundation/Foundation.h> 2 3 @interface Student : NSObject{ 4 NSString *_name; 5 int _age; 6 } 7 @property (copy,nonatomic)NSString *_name; 8 @property (assign,nonatomic)int _age; 9 -(id)init:(NSString *)name andage:(int)age; 10 @end 11 12 @implementation Student 13 @synthesize _name,_age; 14 15 -(id)init:(NSString *)name andage:(int)age{ 16 self = [super init]; 17 18 if(self){ 19 self->_name = [name copy]; 20 self->_age = age; 21 } 22 23 return self; 24 } 25 @end 26 27 @interface NSString (print){ 28 } 29 -(void)print; 30 @end 31 32 @implementation NSString (print) 33 34 -(void)print{ 35 NSLog(@"%@",self); 36 } 37 38 @end 39 40 int main(int argc,char **argv){ 41 @autoreleasepool { 42 NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu",@"zhangsan"]; 43 NSString *str = [array1 objectAtIndex:1]; //查找指定下標的對象 44 NSString *str2 = array1[1]; 45 NSLog(@"str = %@,str2 = %@",str,str2); 46 47 BOOL ishas = [array1 containsObject:@"wangwu"]; //按指定值查找 48 if(ishas) 49 NSLog(@"有"); 50 else 51 NSLog(@"沒有"); 52 53 NSString *str3 = [array1 firstObject]; //獲取array中的第1個數據 54 NSString *str4 = [array1 lastObject]; //獲取array中的最後的數據 55 NSLog(@"str3 = %@,str4 = %@",str3,str4); 56 57 NSUInteger index = [array1 indexOfObject:@"zhangsan"]; //按值找下標,第1個 58 NSLog(@"index = %li",index); 59 60 NSUInteger index2 = [array1 indexOfObject:@"zhangsan" inRange:NSMakeRange(index + 1, [array1 count] - 1)]; //在指定範圍按值查找下標 61 NSLog(@"index2 = %li",index2); 62 } 63 }
5)數組的添加和刪除,註意這裡的添加和刪除並不會對原有數組產生影響,因為它是不可變的,修改後會生成一個新的數組。
1 #import <Foundation/Foundation.h> 2 3 @interface Student : NSObject{ 4 NSString *_name; 5 int _age; 6 } 7 @property (copy,nonatomic)NSString *_name; 8 @property (assign,nonatomic)int _age; 9 -(id)init:(NSString *)name andage:(int)age; 10 @end 11 12 @implementation Student 13 @synthesize _name,_age; 14 15 -(id)init:(NSString *)name andage:(int)age{ 16 self = [super init]; 17 18 if(self){ 19 self->_name = [name copy]; 20 self->_age = age; 21 } 22 23 return self; 24 } 25 @end 26 27 @interface NSString (print){ 28 } 29 -(void)print; 30 @end 31 32 @implementation NSString (print) 33 34 -(void)print{ 35 NSLog(@"%@",self); 36 } 37 38 @end 39 40 int main(int argc,char **argv){ 41 @autoreleasepool { 42 NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu"]; 43 NSArray *array2 = [array1 arrayByAddingObject:@"zhaoliu"]; //將某個對象加入到數組 44 NSLog(@"array2 = %@",array2); 45 46 NSArray *array3 = @[@"aaa",@"bbb",@"ccc"]; 47 NSArray *array4 = [array1 arrayByAddingObjectsFromArray:array3]; //將另一個數組數據加入到一個數組中,組成一個新的數組 48 NSLog(@"array4 = %@",array4); 49 50 // array1[0] = @"abc"; //不可變數組不能刪除 51 NSArray *array5 = [array4 subarrayWithRange:NSMakeRange(3,3)]; //獲得array指定範圍的子數組 52 NSLog(@"array5 = %@",array5); 53 } 54 }
6)數組的排序
1 #import <Foundation/Foundation.h> 2 3 @interface Student : NSObject{ 4 NSString *_name; 5 int _age; 6 } 7 @property (copy,nonatomic)NSString *_name; 8 @property (assign,nonatomic)int _age; 9 -(id)init:(NSString *)name andage:(int)age; 10 -(NSInteger)cmpstuname:(Student *)stu; 11 -(NSInteger)cmpstuage:(Student *)stu; 12 @end 13 14 @implementation Student 15 @synthesize _name,_age; 16 17 -(id)init:(NSString *)name andage:(int)age{ 18 self = [super init]; 19 20 if(self){ 21 self->_name = [name copy]; 22 self->_age = age; 23 } 24 25 return self; 26 } 27 28 -(NSInteger)cmpstuname:(Student *)stu{ 29 return [self._name compare:stu._name]; 30 } 31 32 -(NSInteger)cmpstuage:(Student *)stu{ 33 return self._age - stu._age; 34 } 35 @end 36 37 38 int main(int argc,char **argv){ 39 @autoreleasepool { 40 NSArray *array1 = @[@"zhangsan",@"lisi",@"wangwu",@"zhangsan"]; 41 NSLog(@"before sort array1 = %@",array1); 42 43 NSLog(@"-------------------"); 44 NSArray *array2 = [array1 sortedArrayUsingSelector:@selector(compare:)]; 45 NSLog(@"after sort array2 = %@",array2); 46 47 NSLog(@"-------------------"); 48 NSArray *array3 = [array1 sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) { 49 return [obj1 compare:obj2]; 50 }]; 51 NSLog(@"after sort array3 = %@",array3); 52 53 NSLog(@"-------------------"); 54 Student *stu1 = [[Student alloc] init:@"zhangsan" andage:22]; 55 Student *stu2 = [[Student alloc] init:@"lisi" andage:11]; 56 Student *stu3 = [[Student alloc] init:@"wangwu" andage:33]; 57 NSArray *stuarray = @[stu1,stu2,stu3]; 58 // NSArray *srotstuarr = [stuarray sortedArrayUsingSelector:@selector(cmpstuname:)]; //按名排序 59 NSArray *srotstuarr = [stuarray sortedArrayUsingSelector:@selector(cmpstuage:)]; //按年齡排序 60 for(Student *temp in srotstuarr) 61 NSLog(@"name = %@,age = %i",temp._name,temp._age); 62 63 NSLog(@"----------------"); 64 NSArray *sortstuarr2 = [stuarray sortedArrayUsingComparator:^NSComparisonResult(Student *obj1, Student * obj2) { 65 return [obj1._name compare:obj2._name]; 66 }]; 67 for(Student *temp in sortstuarr2) 68 NSLog(@"name = %@,age = %i",temp._name,temp._age); 69 70 NSLog(@"----------------"); 71 Student *stu4 = [[Student alloc] init:@"zhangsan" andage:11]; 72 Student *stu5 = [[Student alloc] init:@"lisi" andage:22]; 73 Student *stu6 = [[Student alloc] init:@"wangwu" andage:44]; 74 NSArray *stuarray2 = @[stu4,stu5,stu6]; 75 NSArray *allstuarray = [stuarray arrayByAddingObjectsFromArray:stuarray2]; 76 for(Student *temp in allstuarray) 77 NSLog(@"name = %@,age = %i",temp._name,temp._age); 78 79 //使用描述符排序 80 NSSortDescriptor *namescr = [NSSortDescriptor sortDescriptorWithKey:@"_name" ascending:NO]; //第1排序為按名降序排序 81 NSSortDescriptor *agescr = [NSSortDescriptor sortDescriptorWithKey:@"_age" ascending:YES]; //第2排序為按年齡升序排序 82 NSArray *sortallstu = [allstuarray sortedArrayUsingDescriptors:@[namescr,agescr]]; 83 NSLog(@"-------------- "); 84 for(Student *temp in sortallstu) 85 NSLog(@"name = %@,age = %i",temp._name,temp._age); 86 } 87 }
7)分割和連接
1 #import <Foundation/Foundation.h> 2 3 @interface Student : NSObject{ 4 NSString *_name; 5 int _age; 6 } 7 @property (copy,nonatomic)NSString *_name; 8 @property (assign,nonatomic)int _age; 9 -(id)init:(NSString *)name andage:(int)age; 10 @end 11 12 @implementation Student 13 @synthesize _name,_age; 14 15 -(id)init:(NSString *)name andage:(int)age{ 16 self = [super init]; 17 18 if(self){ 19 self->_name = [name copy]; 20 self->_age = age; 21 } 22 23 return self; 24 } 25 26 @end 27 28 29 int main(int argc,char **argv){ 30 @autoreleasepool { 31 NSString *path = @"/Users/user/workspace/OCTest/OCTest"; 32 NSArray *patharray = [path componentsSeparatedByString:@"/"]; //將字元串按指定分隔符進行分割,分割形成數組 33 NSLog(@"patharray = %@",patharray); 34 35 NSString *str = [patharray componentsJoinedByString:@"-"]; //將數組中的數組用指定的分隔符連接成字元串 36 NSLog(@"str = %@",str); 37 } 38 }
2. 可變字元串 (NSMutable)
因為它是 NSArray 的子類,所以它繼承了 NSArray 中的一些方法,所以下麵只介紹它的常用的方法構建和修改。首先先看一下它的方法:
下麵通過一個例子來具體看它的用法:
1)構造
1 #import <Foundation/Foundation.h> 2 3 @interface Student : NSObject{ 4 NSString *_name; 5 int _age; 6 } 7 @property (copy,nonatomic)NSString *_name; 8 @property (assign,nonatomic)int _age; 9 -(id)init:(NSString *)name andage:(int)age; 10 @end 11 12 @implementation Student 13 @synthesize _name,_age; 14 15 -(id)init:(NSString *)name andage:(int)age{ 16 self = [super init]; 17 18 if(self){ 19 self->_name = [name copy]; 20 self->_age = age; 21 } 22 23 return self; 24 } 25 26 @end 27 28 29 int main(int argc,char **argv){ 30 @autoreleasepool { 31 // NSMutableArray *marray = @[@"zhangsan",@"lisi",@"wangwu"]; //錯誤,類型不一致 32 NSMutableArray *marray = [NSMutableArray arrayWithCapacity:12]; 33 marray.array = @[@"zhangsan",@"