【IOS初學者】陣列與字典

mymdeep發表於2017-02-24

在這裡我要說一下,ios開發的知識點都偏於基礎,我只記錄了一些基礎的知識點,並進行了擴充套件,適合入門的朋友:
【IOS初學者】UITableView與自定義UITableViewCell
【IOS初學者】bundle知識點總結
【IOS開發初學者】UINavigationController詳解


NSArray

初始化

   //1)建立一個空陣列
    NSArray *arr1 = [NSArray array];
    //建立陣列,只有一個元素
    NSArray *arr2 = [NSArray arrayWithObject:@"aaa"];
    //建立陣列,有多個元素
    // nil 表示陣列賦值結束
    NSArray *arr3 = [NSArray arrayWithObjects:@"aaa",@"bbb", nil];
    //呼叫物件方法,建立陣列
    NSArray *arr4 = [[NSArray alloc] initWithObjects:@"aaa",@"bbb", nil];
    //用一個陣列可以建立另外一個陣列
    NSArray *arr5 = [NSArray arrayWithArray:arr3];
    //使用@
    NSArray *arr6 = @[@"aaa",@"bbb",@"ccc",@"ddd"];複製程式碼

常用方法

    //獲取陣列的長度
    NSLog(@"%ld",arr3.count);
    //根據下標,獲取下標對應的物件
    NSLog(@"%@",[arr3 objectAtIndex:1]);  
    NSLog(@"%@",arr3[1]);
    //返回元素的下標
    NSUInteger loc = [arr3 indexOfObject:@"bbb"];
    NSLog(@"%ld",loc);
    //陣列中是否包含了某個元素
    if([arr3 containsObject:@"gggg"]){     
        NSLog(@"包含此元素");
    }else{
        NSLog(@"不包含");
    }
    NSLog(@"arr5 = %@",arr5);複製程式碼

對應log:

2017-02-24 09:35:12.836 TestArray[7023:601929] 2
2017-02-24 09:35:12.837 TestArray[7023:601929] bbb
2017-02-24 09:35:12.837 TestArray[7023:601929] bbb
2017-02-24 09:35:12.837 TestArray[7023:601929] 1
2017-02-24 09:35:12.837 TestArray[7023:601929] 不包含
2017-02-24 09:35:12.837 TestArray[7023:601929] arr5 = (
    aaa,
    bbb
)複製程式碼

遍歷

   //普通的方式,通過下標訪問
    for (int i=0; i<arr3.count; i++) {
        NSLog(@"-> %@",arr3[i]);
    }

    // 快速列舉法 for迴圈的增強形式
    for (NSString * str in arr3) {
        NSLog(@"---> %@",str);
    }


    //使用block的方式,進行訪問
    [arr3 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

        if(idx == 2){

            *stop = YES;  //停止

        }else{

            NSLog(@"idx = %ld,obj = %@",idx,obj);
        }


    }];複製程式碼

列印log如下:

2017-02-24 09:46:33.215 TestArray[7047:609202] -> aaa
2017-02-24 09:46:33.215 TestArray[7047:609202] -> bbb
2017-02-24 09:46:33.215 TestArray[7047:609202] ---> aaa
2017-02-24 09:46:33.215 TestArray[7047:609202] ---> bbb
2017-02-24 09:46:33.215 TestArray[7047:609202] idx = 0,obj = aaa
2017-02-24 09:46:33.215 TestArray[7047:609202] idx = 1,obj = bbb
Program ended with exit code: 0複製程式碼

讀寫

 NSArray *array = [NSArray     arrayWithObjects:@"one",@"zbz",@"cgx",@"sb",@"cjk",@"senni", nil];
 BOOL isWrite =  [array writeToFile:@"/Users/xxxx/Desktop/arr.xml" atomically:YES];複製程式碼
 NSArray *readArr = [NSArray arrayWithContentsOfFile:@"/Users/xxxx/Desktop/arr.xml"];複製程式碼

NSArray與字串

  • 陣列拼接字串
 NSArray *arr6 = @[@"aaa",@"bbb",@"ccc",@"ddd"];
    NSString *str = [arr6 componentsJoinedByString:@"-"];
    NSLog(@"str = %@",str);複製程式碼
  • 字串拆分為陣列
NSArray *arr7 = [str componentsSeparatedByString:@"-"];
    NSLog(@"%@",[arr7 firstObject]);
    NSLog(@"%@",[arr7 lastObject]);
    NSLog(@"%@",arr7[1]);複製程式碼

看下列印log:

2017-02-24 09:58:04.852 TestArray[7107:619117] str = aaa-bbb-ccc-ddd
2017-02-24 09:58:04.852 TestArray[7107:619117] aaa
2017-02-24 09:58:04.853 TestArray[7107:619117] ddd
2017-02-24 09:58:04.853 TestArray[7107:619117] bbb
Program ended with exit code: 0複製程式碼

NSMutableArray

初始化

  NSMutableArray *arr1 = [NSMutableArray array];
    //建立的時候初始化一個元素
    NSMutableArray *arr2 = [NSMutableArray arrayWithObject:@"aaa"];

    //建立陣列的時候,初始化多個元素
    NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"bbb",@"ccc",@"ddd",nil];

    //建立一個陣列,並且指定長度,
    NSMutableArray *arr4 = [NSMutableArray arrayWithCapacity:5];複製程式碼

操作

  • 新增

    [arr1 addObject:@"ccc"];
    [arr1 insertObject:@"ddd" atIndex:0];複製程式碼
  • 刪除

    /刪除指定元素
    [arr1 removeObject:@"aaa"];
    //根據角標刪除元素
    [arr1 removeObjectAtIndex:1];
    //刪除所有元素
    [arr1 removeAllObjects];複製程式碼
  • 修改

    [arr3 replaceObjectAtIndex:1 withObject:@"aaa"];
    arr3[1] = @"ccc";複製程式碼
  • 查詢
    BOOL isSearch = [arr3 containsObject:@"ddd"];
    NSLog(@"%d",isSearch);複製程式碼
  • 交換
    NSMutableArray *arr5 =[NSMutableArray arrayWithObjects:@1,@2,@3,@4,@5, nil];
    [arr5 exchangeObjectAtIndex:0 withObjectAtIndex:4];
    NSLog(@"%@",arr5);複製程式碼

    遍歷

    NSMutableArray的遍歷可以使用NSArray的方式進行遍歷

NSDictionary

建立

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"lnj", @"name", @"12345678", @"phone", @"天朝", @"address", nil];

NSDictionary *dict = @{@"name":@"lnj", @"phone":@"12345678", @"address":@"天朝"};複製程式碼

獲取元素

[dict objectForKey:@"name”];
dict[@"name”];複製程式碼

遍歷

   NSDictionary *dict = @{@"name":@"lnj", @"phone":@"12345678", @"address":@"天朝"};
    for (NSString *key in dict) {
        NSLog(@"key = %@, value = %@", key, dict[key]);
    }複製程式碼

  [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
        NSLog(@"key = %@, value = %@", key, obj);
    }];複製程式碼

讀寫

  • NSDictionary *newDict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/LNJ/Desktop/dict.plist"];
      NSLog(@"newDict = %@", newDict);複製程式碼
  • NSDictionary *dict = @{@"name":@"lnj", @"phone":@"12345678", @"address":@"天朝"};
      BOOL flag = [dict writeToFile:@"/Users/LNJ/Desktop/dict.plist" atomically:YES];
      NSLog(@"flag = %i", flag);複製程式碼

NSMutableDictionary

初始化

NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1",@"key1",@"v2",@"key2",nil];
 NSMutableDictionary *dict1 = [NSMutableDictionary dictionary]; 
  NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithCapacity:3];複製程式碼

新增元素

[dic2 addEntriesFromDictionary:dic3];
[dic2 setValue:@"value3" forKey:@"key3"];複製程式碼

刪除元素

//刪除指定key
[dic1 removeObjectForKey@"key1"];
//刪除指定key陣列
NSArray *array = [NSArray arrayWithObjects:@"key1", nil];
[dic2 removeObjectsForKeys:array];
//移除字典所有物件
[dic1 removeAllObjects];複製程式碼

修改元素

 [dict1 setObject:@"zhaosi" forKey:@"ls"];
  //簡寫形式
  dict1[@"ls"] = @"xxxxx";
   NSLog(@"%@",dict1);複製程式碼

遍歷

 NSMutableDictionary *dictionary3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"value",@"key",@"value2",@"key2",@"value3",@"key3",@"value4",@"key4",@"value5",@"key5",@"value6",@"key6", nil];
        for (id key in dictionary3) {
            id obj = [dictionary3 objectForKey:key];

            NSLog(@"快速列舉--->obj= %@",obj);
        }

        //一般列舉
        NSArray *keys = [dictionary3 allKeys];
        float length = [keys count];
        for (int i = 0; i < length; i ++) {
            id key = [keys objectAtIndex:i];
            id obj = [dictionary3 objectForKey:key];
            NSLog(@"一般列舉--->%@",obj);
        }

        //通過列舉類列舉
        NSEnumerator *enumerator3 = [dictionary3 keyEnumerator];
        id key = [enumerator3 nextObject];
        while (key) {
            id obj = [dictionary3 objectForKey:key];
            NSLog(@"通過列舉類列舉--->%@",obj);
            key = [enumerator3 nextObject];
        }複製程式碼

相關文章