【IOS初學者】數組與字典

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


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:java

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以下:ios

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];複製代碼

操做

  • 添加post

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

    /刪除指定元素
    [arr1 removeObject:@"aaa"];
    //根據角標刪除元素
    [arr1 removeObjectAtIndex:1];
    //刪除全部元素
    [arr1 removeAllObjects];複製代碼
  • 修改spa

    [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]);
    }複製代碼

code

[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];
        }複製代碼
相關文章
相關標籤/搜索