iOS开发-object-c之 @[], @{}

 今天看别人代码的时候发现这样的用法

navigationController.viewControllers = @[secondViewController];

这里用到了@[]。

刚开始不是很明白,问了才知道,原来这是数组的初始化。是ios6之后引入的方式。mark下。

@[]  初始化不可变数组

@{} 初始化不可变字典




举例如下:

     view plaincopy在CODE上查看代码片派生到我的代码片

      NSArray *testArr = @[@"a", @"b", @"c", @"d"];  
          
        NSString *testStr = testArr[2];  
        NSLog(@"testArr========%@",testStr);  
          
        NSDictionary *testDic = @{@"a": @"first",  
                                  @"b": @"second",  
                                  @"c": @"third"  
                                  };  
        testStr = testDic[@"a"];  
        NSLog(@"testDic========%@",testStr);</strong>  

打印结果:
 view plaincopy在CODE上查看代码片派生到我的代码片

    <strong>2014-01-16 10:23:18.991 VC_Frame[1175:a0b] testArr========c  
    2014-01-16 10:23:18.992 VC_Frame[1175:a0b] testDic========first</strong>  
原文地址:https://www.cnblogs.com/Free-Thinker/p/4995095.html