【IOS初学者】数组与字典

在这里我要说一下,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];
        }
    原文作者:mymdeep
    原文地址: https://www.jianshu.com/p/878873a7eb07
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞