日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

OC中的NSArray和NSMutableArray、NSDictionary和NSMutableDictionary用法

發布時間:2023/11/27 生活经验 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OC中的NSArray和NSMutableArray、NSDictionary和NSMutableDictionary用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一:NSArray 和NSMutableArray 1: NSArray:不可變數組 NSArray是OC中使用的數組,只能用來存放OC對象,不能存放非OC對象如基本數據類型 它使不可變的,一旦初始化完畢,內容不能改變,也不能添加元素。 而C語言中的數組只能存放一種數據類型 (1) 普通數組的用法
// 普通數組的創建
//        int arr[5] = {1,3,4};// 對象數組的創建
//        Person *p = [[Person alloc] init];
//        Person *arrP[5] = {p, [[Person alloc] init]};

(2):NSArray的創建

 
        // 0,創建的數組對象永遠都是空數組,也就沒有任何意義,因為NSArray是不可變的
//        NSArray *arr1 = [NSArray array];// 1, 創建只有一個對象的數組
//        NSArray *arr2 = [NSArray arrayWithObject:p];
//        NSLog(@"%ld", arr2.count); // 結果 1// 2, 創建多了對象的數組,注意nil必須填寫,數組元素什么時候結束
//        NSArray *arr3 = [NSArray arrayWithObjects:@"12", @"34", nil];
//        NSLog(@"%ld", arr3.count); //結果:2// 3,快速創建一個NSArray
//        NSArray *arr4 = @[@"12", @"34"];
//        NSLog(@"%@", arr4); // 結果: (12,34)

(3)OC數組的訪問

// 獲取數組中元素的個數
//        NSArray *arr5 = @[@"hello", @"world", @"china"];
//        NSLog(@"%ld", arr5.count); // 結果 3
//        
//        // 訪問數組中指定位置的元素
//        NSString *str = [arr5 objectAtIndex:2];
//        NSLog(@"%@", str); // china
//        
//        // 快速訪問數據指定位置的元素值
//        NSString *str2 = arr5[1];
//        NSLog(@"%@", str2); // 結果:world
//        
//        // 查找指定元素的位置
//        NSUInteger index = [arr5 indexOfObject:@"hello"];
//        NSLog(@"index = %ld", index); // 結果 0;// 獲取第一個元素
//          id str = [arr5 firstObject];
//          NSLog(@"%@", str);//  獲取最后一個元素
//        id  str = [arr5 lastObject];
//        NSLog(@"%@", str);
//        
//        // 判斷數組中是否存在摸個元素
//        BOOL b = [arr5 containsObject:@"hello"];
//        NSLog(@"%d", b);

(4)OC數組的遍歷

// 1,通過for循環簡單遍歷
//        NSArray *arr6 = @[@"hello", @"china", @"world"];
////        for (int i = 0; i<arr6.count; i++) {
//            NSLog(@"%@", [arr6 objectAtIndex:i]);
////            NSLog(@"%@", arr6[i]);
////        }
//        
//        // 2,通過for in進行遍歷
////        for (NSString *str in arr6) {
////            NSLog(@"%@", str);
////        }
//        
//        // 3,通過數組的方法 Block
//        [arr6 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//            if (idx == 1) {
//                *stop = YES;
//            }
//            NSLog(@"%@----%ld", obj, idx);
//        }];/***  這個方法是每遍歷到一個元素,就把這個元素還有索引當做一個參數傳給Block*/// enumerateObjectsUsingBlock遍歷的深入研究,內部是怎么實現的
//        void(^myBlock)(id obj, NSUInteger idx, BOOL *stop) = ^(id obj, NSUInteger idx, BOOL *stop){
//            NSLog(@"%@----%ld", obj, idx);
//            if (idx == 1) {
//                *stop = YES;
//            }
//        };
//        for (int i = 0; i<arr6.count; i++) {
//            
//            id obj = arr6[i];
//            BOOL isStop = NO;
//            myBlock(obj, i, &isStop);
//            if (isStop) {
//                
//                break;
//            }
//        }

(5)OC數組給數組中的所有元素發送消息及帶一個參數的消息

Person *p1 = [[Person alloc] init];
//        p1.name = @"張伯倫";
//        
//        Person *p2 = [[Person alloc] init];
//        p2.name = @"喬丹";
//
//        Person *p3 = [[Person alloc] init];
//        p3.name = @"奧尼爾";
//
//        NSArray *arr = @[p1, p2, p3];
//        [arr makeObjectsPerformSelector:@selector(sayHi)];
//        [arr makeObjectsPerformSelector:@selector(say:) withObject:@"你好"];/***  這個方法會向數組中存放的每一個對象發送消息,不過只能有一個參數的消息方法*/

(6)OC數組的文件夾的讀取操作:

        // NSArray 只能寫入字符串類型的數據,,NSDictionary才可以寫對象到文件,在蘋果開發中,遇到xml文件,可以把后綴該為.plist,這樣就可以使用xcode打開編輯了。
//        NSArray *arr6 = @[@"hello", @"china", @"world"];
//        NSLog(@"%d",[arr6 writeToFile:@"/Users/ll/Desktop/text.plist" atomically:YES]);
//        NSArray *arr = [NSArray arrayWithContentsOfFile:@"/Users/llDesktop/text.plist" ];
//        NSLog(@"%@", arr);

(7)NSString和NSArray

        NSArray *array = @[@"喬丹", @"科比", @"加內特"];// 將數組用一個指定的字符串拼接起來
//        NSString *str = [array componentsJoinedByString:@"NBA"];// 將字符串帶??字符串分割成一個數組
//        NSString *str = @"喬丹??科比??加內特";
//        NSArray *array2 = [str componentsSeparatedByString:@"??"];
//        NSLog(@"%@", array2);

?

2 > NSMutableArray的用法 創建添加刪除元素(它是一個可變數組)
        // 創建一個可變數組// 添加元素
//        NSMutableArray *arrM = [NSMutableArray arrayWithObject:@"world"];
//        [arrM addObject:@"hello"];
//        
//        // OC數組不能添加nil對象 錯誤
////        [arrM addObject:nil];
//        
//        // 刪除元素
////        [arrM removeObject:@"hello"];
////        [arrM removeAllObjects];
//          // 刪除指定位置的元素
//        [arrM removeObjectAtIndex:1];
//        NSLog(@"%@", arrM);// 替換元素
//        NSArray *array1 = @[@"a", @"b", @"c"];
//        NSMutableArray *arrM = [NSMutableArray arrayWithArray:array1];
//        [arrM replaceObjectAtIndex:1 withObject:@"m"];
//        [arrM exchangeObjectAtIndex:1 withObjectAtIndex:2];
//        NSLog(@"%@", arrM);
注意:
  • NSMutableArray *array = @[@"bob", @"steve", @"john"];

  • [array addObject:@“Peter”];//?錯誤,使?用@[@"bob", @"steve", @"john"]這種?方式 創建的永遠是NSArray(不可變數組)

  • //正確

  • NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"bob",

    @"steve", @"john"]];

  • [array addObject:@"Peter”];

    二:NSDictionary

    什么是NSDictionary

    ??NSDictionary翻譯過來叫做”字典”

    ???日常?生活中, “字典”的作?用:?通過?一個拼?音或者漢字,?就能找到對應的詳細解 釋

    ??NSDictionary的作?用類似:?通過?一個key,?就能找到對應的value???NSDictionary是不可變的,??一旦初始化完畢,??里?面的內容就?無法修改

    1)不可變字典的創建

  •         // 1,創建字典及初始化, 不可變// 沒有任何意義
    //        NSDictionary *dic = [NSDictionary dictionary]; //// 創建只有一個鍵值對的字典
    //        NSDictionary *dic = [NSDictionary dictionaryWithObject:@"yaoming"
    //            forKey:@"ym"];// 創建多個鍵值對字典
    //        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"liuxiang", @"lx", @"tianliang", @"tl", nil];//        // 快速創建方法
    //        NSDictionary *dic = @{@"ym":@"yaoming", @"lx":@"liuxiang"};
    //        NSLog(@"%@", dic);

    2)字典的訪問

  • // 2,字典訪問
    //        NSDictionary *dict = @{@"zs" : @"zhangsan", @"ls" : @"lisi", @"ww" : @"Wangwu"};// 返回字典中的鍵值數目 count
    //        NSUInteger length = dict.count;// 根據鍵求值
    //        id str = [dict objectForKey:@"zs"];// 快速訪問
    //        id str = dict[@"zs"];
    //        NSLog(@"str = %@", str);

    3)字典的遍歷

  •         // 3,字典的遍歷
    //        NSDictionary *dict = @{@"zs" : @"zhangsan", @"ls" : @"lisi", @"ww" : @"Wangwu"};// for循環
    //        id key = [dict allKeys]; // 可以知道字典中所有的值
    //        for (int i = 0; i<dict.count; i++) {
    //            
    //            id str = dict[key[i]];
    //            NSLog(@"%@:%@", key[i], str);
    //        }// for in遍歷字典中的所有的鍵
    //        for (NSString  *key in dict) {
    //            
    //            id str = dict[key];
    //            NSLog(@"%@:%@", key, str);
    //        }// Block
    //        [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    //            NSLog(@"%@:%@", key, obj);
    //        }];

    4)字典讀取文件的操作

  •         // 4,把字典寫入到文件
    //        NSDictionary *dict = @{@"zs" : @"zhangsan", @"ls" : @"lisi", @"ww" : @"Wangwu"};
    //        
    //        [dict writeToFile:@"/Users/ll/Desktop/te.plist" atomically:YES];// 用字典讀取文件
    //        NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:@"/Users/ll/Desktop/te.plist"];
    //        NSLog(@"%@", dic);
  • ?

    注意:字典中的鍵不要重復。(雖然重復也不報錯,會?自動取在前?面的那個)

    2,NSMutableDictionary

    ??什么是NSMutableDictionary

    ??NSMutableDictionary是NSDictionary的?子類

    ??NSDictionary是不可變的,??一旦初始化完畢后,?它?里?面的內容就永遠是固定 的,?不能刪除?里?面的元素,?也不能再往?里?面添加元素

    ??NSMutableDictionary是可變的,?隨時可以往?里?面添加\更改\刪除元素

    ?NSMutableDictionary的用法

            // 1,創建一個空的可變字典NSMutableDictionary *dicM = [NSMutableDictionary dictionary];[dicM setObject:@"zhangsan" forKey:@"zs"];// 2,刪除指定鍵值對
    //        [dicM removeObjectForKey:@"zs"];// 3,刪除字典中所有內容
            [dicM removeAllObjects];NSLog(@"%@", dicM);/***  注意:@{鍵:值}創建的字典為不可變字典 ,不用賦值給可變字典及后續操作*/

    ?

轉載于:https://www.cnblogs.com/-boy/p/4096453.html

總結

以上是生活随笔為你收集整理的OC中的NSArray和NSMutableArray、NSDictionary和NSMutableDictionary用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。