IOS开发学习笔记(十一)——ObjectC中集合类型的使用

对比Java,ObjectC中的类型目前我认识到的要复杂一些。Java中的集合类型包括数组、List、Set、Map等等。。。我们下面一步一步看看ObjectC中的集合类型。


数组类型


ObjectC中的数组类型包括两种:C格式的和ObjectC格式的。

C格式的数组

C格式的数据类型如下:    

        int array[5] = {1, 2, 3, 4, 5};
        NSLog(@"the 2nd number of the array is %i", array[1]);
        NSString *arrayStr[5] = {@"America", @"Britain", @"China", @"Dutch", @"Egypt"};
        NSLog(@"the 2nd number of the array is %@", arrayStr[1]);

这种数组类型有如下限制:

  1. 没有边界检查。貌似java也没有,毕竟访问都是动态的;
  2. size是固定的。因为不是使用new动态生成的;
  3. 不能混用不同类型,因为都是简单数据类型的数组;

ObjectC格式的NSArray数组

示例代码如下:

        NSArray *array02 = [[NSArray alloc] initWithObjects:@"America", @"Britain", @"China", @"Dutch", @"Egypt", nil];
        NSLog(@"the 2nd number of the array is %@", [array02 objectAtIndex:1]);
        
        array02 = [NSArray arrayWithObjects:@"America", @"Britain", @"China", @"Dutch", @"Egypt", nil];
        NSLog(@"the 2nd number of the array is %@", [array02 objectAtIndex:1]);

通过帮助我们可以看到NSArray的特点:

  1. 解决了混用不同类型的问题,因为我们初始化的时候添加的是一个个的对象;
  2. 每个数组本身是不可变的(immutable,注意不是引用数组的变量),因为是通过初始化方法初始化数组内容的,并且没有add或者insert等等方法;

ObjectC格式的NSMutableArray

示例代码如下:

        NSMutableArray *array03 = [[NSMutableArray alloc] initWithObjects:@"America", @"Britain", @"China", @"Dutch", @"Egypt", nil];
        NSLog(@"the 2nd menber of the array is %@", [array03 objectAtIndex:1]);
        
        array03 = [NSMutableArray arrayWithObjects:@"America", @"Britain", @"China", @"Dutch", @"Egypt", nil];
        NSLog(@"the 2nd menber of the array is %@", [array03 objectAtIndex:1]);
        
        [array03 addObject: @"France"];
        [array03 removeObjectAtIndex:1];
        NSLog(@"the 2nd menber of the array is %@", [array03 objectAtIndex:1]);
        // 显示最后一个元素
        NSLog(@"the last menber of the array is %@", [array03 objectAtIndex:[array03 count] - 1]);

这次我们可以看到:

  1. NSMutableArray解决了NSArray不可变的问题,可以在其中新增、删除内容;


Dictionary类型

dictionary相当于java中的set,采用key-value键值对的形式。

NSDictionary

示例演示给NSDictionary赋值和通过键获取值:

        NSDictionary *dic01 = [NSDictionary dictionaryWithObjectsAndKeys:@"America", @"us", @"Britain", @"uk", @"China", @"cn", nil];
        NSLog(@"the selected element is %@", [dic01 objectForKey:@"us"]);

NSMutableDictionary

有NSDictionary,那么也有可变的NSMutableDictionary,下面演示赋值、添加元素、删除元素、查找元素:

        NSMutableDictionary *dic02 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"America", @"us", @"Britain", @"uk", @"China", @"cn", nil];
        [dic02 setObject:@"France" forKey:@"fr"];
        [dic02 removeObjectForKey:@"us"];
        NSLog(@"the selected element is %@", [dic02 objectForKey:@"fr"]);


快速枚举

实际上ObjectC的这些功能对于有过C++或者Java经验的我来说理解上会存在一定的障碍。尽管我能知晓代码是如何写出来的并且能够理解代码的含义,但我始终觉得在没有加入泛型的情况下使用ObjectC中的快速枚举有些许的不够严谨。我们继续查看代码。下面是一个对于Dictionary使用快速枚举的例子:

        NSMutableDictionary *dic02 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"America", @"us", @"Britain", @"uk", @"China", @"cn", nil];
        for (NSString *nation in dic02) {
            NSLog(@"the selected element is %@, and fullname is %@", nation, [dic02 objectForKey: nation]);
        }
        [dic02 setObject:@"France" forKey:@"fr"];
        [dic02 removeObjectForKey:@"us"];
        for (NSString *nation in dic02) {
            NSLog(@"the selected element is %@, and fullname is %@", nation, [dic02 objectForKey: nation]);
        }






















  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值