iOS開發之OC篇(3)—— NSArray、NSMutableArray
版本
一、不可變陣列NSArray
老規矩,先貼上知識點,再打個例子和舉個比方。
//使用arrayWithObjects建立NSArray
NSArray *arr = [NSArray arrayWithObjects:@"abc",@"def",@"123",nil];
// NSArray *arr = [[NSArray alloc] initWithObjects:@"111",@"ddd",@"234",nil];
//使用arrayWithArray建立NSArray
NSArray *arr1 = [NSArray arrayWithArray:arr];
// NSArray *arr1 = [[NSArray alloc]initWithArray:arr];
//使用@建立NSArray,只能建立不可變陣列
NSArray *arr2 = @[@"aaaa",@"bbb",@"ddddd"];
NSLog(@"arr2 = %@",arr2);
//返回元素個數
NSLog(@"arr2 count = %ld",[arr2 count]);
//返回指定位置id
NSString *str0 = [arr2 objectAtIndex:2];
NSLog(@"str0 = %@",str0);
//訪問陣列的最後一個元素
NSString *str = [arr2 lastObject];
NSLog(@"str = %@",str);
//訪問陣列的第一個元素
NSString *str1 = [arr firstObject];
NSLog(@"str1 = %@",str1);
//判斷陣列內是否含有某一個物件
if([arr containsObject:@"123"]) {
NSLog(@"含有此物件");
}
//獲取某個物件在陣列中的下標值
NSUInteger index = [arr indexOfObject:@"123"];
//陣列的遍歷
for(NSInteger i =0;i < [arr count]; i++) {
NSLog(@"arr[%ld] = %@",i,arr[i]);
}
//快速列舉法(快速遍歷)
for(id objin arr) {
NSLog(@"obj = %@",obj);
}
- int main(int argc, charchar * argv[]) {
- //使用arrayWithObjects建立NSArray
- NSArray *arr = [NSArray arrayWithObjects:@"ABC",@"DEF",@"GHI",nil];
- //NSArray *arr = [[NSArray alloc] initWithObjects:@"ABC",@"DEF",@"GHI",nil];
- NSLog(@"arr = %@",arr);
- //使用arrayWithArray建立NSArray
- NSArray *arr1 = [NSArray arrayWithArray:arr];
- //NSArray *arr1 = [[NSArray alloc]initWithArray:arr];
- NSLog(@"arr1 = %@",arr1);
- //使用@建立NSArray,只能建立不可變陣列
- NSArray *arr2 = @[@"123",@"456",@"789"];
- NSLog(@"arr2 = %@",arr2);
- //返回元素個數
- NSLog(@"arr count = %ld",[arr count]);
- //返回指定位置id
- NSString *str = [arr objectAtIndex:1];
- NSLog(@"str = %@",str);
- //訪問陣列的第一個元素
- NSString *strF = [arr firstObject];
- NSLog(@"strF = %@",strF);
- //訪問陣列的最後一個元素
- NSString *strL = [arr lastObject];
- NSLog(@"strL = %@",strL);
- //判斷陣列內是否含有某一個物件
- if ([arr2 containsObject:@"456"]) {
- NSLog(@"陣列arr2內含有物件456");
- }
- //獲取某個物件在陣列中的下標值
- NSUInteger index = [arr indexOfObject:@"ABC"];
- NSLog(@"ABC下標值為%ld",index);
- //陣列的遍歷
- for (NSInteger i = 0; i < [arr count]; i++) {
- NSLog(@"arr[%ld] = %@",i,arr[i]);
- }
- //快速列舉法(快速遍歷)
- for (id arr2Q in arr2) {
- NSLog(@"arr2Q = %@",arr2Q);
- }
- }
輸出結果:
二、可變陣列NSMutableArray
//例項化一個空的可變陣列
NSMutableArray *nsarr = [[NSMutableArray alloc]init];
//新增一個元素
[nsarr addObject:@"ONE"];
NSLog(@"nsarr = %@",nsarr);
//新增多個元素
[nsarr addObjectsFromArray:@[@"ONE",@"TWO",@"THREE"]];
NSLog(@"nsarr = %@",nsarr);
//插入元素
[nsarr insertObject:@"Two" atIndex:1];
NSLog(@"nsarr = %@",nsarr);
//替換元素
[nsarr replaceObjectAtIndex:0 withObject:@"1"];
NSLog(@"nsarr = %@",nsarr);
//查詢元素
NSString *result = [nsarr objectAtIndex:1];//arr[1]
NSLog(@"result = %@",result);
//刪除指定元素
[nsarr removeObject:@"ONE"];
NSLog(@"nsarr = %@",nsarr);
//刪除指定下標的元素
[nsarr removeObjectAtIndex:2];
NSLog(@"nsarr = %@",nsarr);
//刪除最後一個元素
[nsarr removeLastObject];
NSLog(@"nsarr = %@",nsarr);
//刪除所有元素
[nsarr removeAllObjects];
NSLog(@"nsarr = %@",nsarr);
//刪除陣列中含有的所有元素
[nsarr removeObjectsInArray:@[@"1",@"THREE"]];
//交換元素
NSMutableArray *nsarr1 = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",nil];
[nsarr1 exchangeObjectAtIndex:1 withObjectAtIndex:2];
NSLog(@"nsarr1 = %@",nsarr1);
//切割字串
NSString *str3 = @"I am a super man + beautiful, yeah!";
//用指定的字串進行切割
NSArray *rArr = [str3 componentsSeparatedByString:@" "];
NSLog(@"rArr = %@",rArr);
//例項化一個字符集合
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@" ,+"];
//按照字符集合所提供的字元進行切割
NSArray *rArr2 = [str3 componentsSeparatedByCharactersInSet:set];
NSLog(@"rArr2 = %@",rArr2);
//拼接陣列內的字串
NSString *str2 = [rArr2 componentsJoinedByString:@"-"];
NSLog(@"str2 = %@",str2);
//按照ASCII碼大小進行排序,SEL:方法選擇器(儲存一個方法名)
NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"B",@"A",@"C",@"Z",nil];
[arr3 sortedArrayUsingSelector:@selector(compare:)];//方法選擇器
//按三個人年齡大小排序
Person *per0 = [[Person alloc]initWith:18];
Person *per1 = [[Person alloc]initWith:6];
Person *per2 = [[Person alloc]initWith:60];
NSMutableArray *perArr = [[NSMutableArray alloc]initWithObjects:per0,per1,per2,nil];
//compareAge提供一個排序方法,看按什麼方式排序
NSArray *resultPerArr = [perArr sortedArrayUsingSelector:@selector(compareAge:)];
- int main(int argc, charchar * argv[]) {
- //例項化一個空的可變陣列
- NSMutableArray *arr = [[NSMutableArray alloc]init];
- //新增一個元素
- [arr addObject:@"he"];
- NSLog(@"arr = %@",arr);
- //新增多個元素
- [arr addObjectsFromArray:@[@"is",@"Chinese"]];
- NSLog(@"arr = %@",arr);
- //插入元素
- [arr insertObject:@"not" atIndex:2];
- NSLog(@"arr = %@",arr);
- //以arrayWithArray建立arr1
- NSMutableArray *arr1 = [NSMutableArray arrayWithArray:arr];
- //替換元素
- [arr replaceObjectAtIndex:3 withObject:@"Japanese"];
- NSLog(@"arr = %@",arr);
- //查詢元素
- NSString *result = [arr objectAtIndex:0];
- NSLog(@"arr[0] = %@",result);
- //刪除指定元素
- [arr removeObject:@"not"];
- NSLog(@"arr = %@",arr);
- //刪除指定下標的元素
- [arr removeObjectAtIndex:1];
- NSLog(@"arr = %@",arr);
- //刪除最後一個元素
- [arr removeLastObject];
- NSLog(@"arr = %@",arr);
- //刪除所有元素
- [arr removeAllObjects];
- NSLog(@"arr = %@",arr);
- //刪除陣列中含有的所有元素
- [arr1 removeObjectsInArray:@[@"not",@"is"]];
- NSLog(@"arr1 = %@",arr1);
- //交換元素
- [arr1 exchangeObjectAtIndex:0 withObjectAtIndex:1];
- NSLog(@"arr1 = %@",arr1);
- //切割字串
- NSString *str1 = @"I am,a super man!";
- //用指定的字串進行切割
- NSArray *cutArr = [str1 componentsSeparatedByString:@","];
- NSLog(@"cutArr = %@",cutArr);
- //例項化一個字符集合
- NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"super "];
- //按照字符集合所提供的字元進行切割
- NSArray *cutArr2 = [str1 componentsSeparatedByCharactersInSet:set];
- NSLog(@"cutArr2 = %@",cutArr2);
- //拼接陣列內的字串
- NSString *str2 = [cutArr2 componentsJoinedByString:@"-"];
- NSLog(@"str2 = %@",str2);
- /*
- //按照ASCII碼大小進行排序,SEL:方法選擇器(儲存一個方法名)
- NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"B",@"A",@"C",@"Z",nil];
- [arr3 sortedArrayUsingSelector:@selector(compare:)]; //方法選擇器
- //按三個人年齡大小排序
- Person *per0 = [[Person alloc]initWith:18];
- Person *per1 = [[Person alloc]initWith:6];
- Person *per2 = [[Person alloc]initWith:60];
- NSMutableArray *perArr = [[NSMutableArray alloc]initWithObjects:per0,per1,per2,nil];
- //compareAge提供一個排序方法,看按什麼方式排序
- NSArray *resultPerArr = [perArr sortedArrayUsingSelector:@selector(compareAge:)];
- */
- }
輸出結果:
相關文章
- 13. iOS開發小細節--OC篇iOS
- OC-NSArray的基本介紹
- iOS 開發選擇OC還是Swift?iOSSwift
- iOS開發中使用OC和swift的對比iOSSwift
- Flutter混合開發—iOS篇FlutteriOS
- ios 開發UI篇—UITextViewiOSUITextView
- iOS開發之FuckingBlockSyntax!iOSBloC
- [iOS][OC] 開發利器:控制器傳送門VCPicker(附demo)iOS
- Flutter外掛開發---iOS篇FlutteriOS
- iOS-OC、Swift混編之橋接檔案iOSSwift橋接
- iOS開發之逆向工程iOS
- iOS之Wifi開發探究iOSWiFi
- iOS開發小記-基礎篇iOS
- iOS之BLE藍芽SDK開發個人總結(進階篇)iOS藍芽
- iOS之BLE藍芽SDK開發個人總結(基礎篇)iOS藍芽
- iOS開發UI篇--iOS動畫(Core Animation)總結iOSUI動畫
- iOS面試旗開得勝之答案篇iOS面試
- iOS 初探程式碼混淆(OC)iOS
- iOS開發原始碼閱讀篇--FMDB原始碼分析3(FMDatabaseQueue+FMDatabasePool)iOS原始碼Database
- iOS開發之彈窗管理iOS
- iOS開發之 Autolayout 詳解iOS
- iOS開發之XLForm的使用iOSORM
- iOS開發UI篇--UICollectionView初步入門iOSUIView
- iOS混合開發庫(GICXMLLayout)五、Texture篇iOSXML
- iOS混合開發庫(GICXMLLayout)七、JavaScript篇iOSXMLJavaScript
- iOS開發經驗總結3iOS
- iOS開發備忘筆記 (3)iOS筆記
- iOS OC-自定義TabBar TabBarViewControlleriOStabBarViewController
- iOS UMeng OC和Swift混編iOSSwift
- 輕鬆教你React Native 混合開發(iOS篇)React NativeiOS
- iOS開發基礎篇--NSNotificationCenter使用小結iOS
- ionic開發環境搭建之ios開發環境iOS
- iOS開發之UIRefreshControl使用踩坑iOSUI
- 騰訊音樂招 iOS 開發, base 深圳,要求:本科、三年、OC,懂音視訊開發優先。iOS
- iOS開發實用軟體之NWPusheriOS
- iOS開發之 Method Swizzling 深入淺出iOS
- iOS開發之登入與訪客iOS
- flutter開發之Dart[必讀篇]?FlutterDart