从xcode4.4开始,LLVM4.0编译器为Objective-C添加一些新的特性。创建数组NSArray,哈希表NSDictionary, 数值对象NSNumber时,可以像NSString的初始化一样简单方便。妈妈再也不担心程序写得手发酸了。

有兴趣的朋友可以关注LLVM编译器的相关文档:http://clang.llvm.org/docs/ObjectiveCLiterals.html

关于NSDictionary和NSNumber的例子来自:http://cocoaheads.tumblr.com/post/17757846453/objective-c-literals-for-nsdictionary-nsarray-and

I. NSArray

首先是非常常用的NSArray,NSMutableArray。NSArray是一个初始化后就固定的静态数组。如果想对数组的元素进行插入,删除,更新等操作,就得使用Objective-C 的动态数组NSMutableArray。

在LLVM4.0之前,NSArray的初始化方法如下。注: 以下的方法在LLVM4.0之后也可以继续使用。

//LLVM4.0之前 NSArray的初始化

NSArray *oldOne = [NSArray arrayWithObjects:@"1st", @"2nd", @"3th", nil];

// 取得数组第2个值

NSString *s = [oldOne objectAtIndex:1];

在LLVM4.0之后,NSArray的初始化方法如下。

NSArray *newOne =@[@"1st", @"2nd", @"3th”];

// 取得数组第2个值

NSString *s = newOne[1];

特别要说一下NSMutableArray。LLVM4.0之前,如果你要更新数组的某个元素,一般使用下面的方法。

//LLVM4.0之前 NSMutableArray的初始化

NSMutableArray *oldMutable = [NSMutableArray arrayWithArray: old];

[mutable replaceObjectAtIndex:1 withObject:@"disposed"]; //更新某个元素

在编写一些常用算法时,下面的特性导致编写起来有一点麻烦。

/*想更新NSMutableArray的某个元素?请先初始化这个元素*/

NSMutableArray *oldMutable = [[NSMutableArray alloc] init]];

/*必须如下给每个元素赋一个初值,否则exception会发生

for (int h = 0; h < 5; h++) {

[oldMutable addObject:@"1"];

}

@try{

[mutable replaceObjectAtIndex:1 withObject:@"disposed"];

}

@catch(NSException *exception){

NSLog(@“%@“, [exception description]);

}

这而LLVM4.0简化了这一个过程,可以用如下方式简单完成。

//LLVM4.0之后

NSMutableArray *newMutable = [NSMutableArray alloc] init];

newMutable[2] = @"myObject";

突然觉得世界清爽了不少,对于熟悉C语言和java等的童鞋来说,是不是觉得亲切多了?

关于NSDictionary和NSNumber的变化如下, 就不细说啦。

II.NSDictionary

一般性的写法:

dict = [NSDictionary dictionaryWithObjects:@[o1, o2, o3]

forKeys:@[k1, k2, k3]];

LLVM4.0之前后:

dict = @{ k1 : o1, k2 : o2, k3 : o3 };

III. NSNumber

一般性的写法:

NSNumber *number;

number = [NSNumber numberWithChar:'X'];

number = [NSNumber numberWithInt:12345];

number = [NSNumber numberWithUnsignedLong:12345ul];

number = [NSNumber numberWithLongLong:12345ll];

number = [NSNumber numberWithFloat:123.45f];

number = [NSNumber numberWithDouble:123.45];

number = [NSNumber numberWithBool:YES];

LLVM4.0之前后:

NSNumber *number;

number = @'X';

number = @12345;

number = @12345ul;

number = @12345ll;

number = @123.45f;

number = @123.45;

number = @YES;

Objective-C中关于NSArray, NSDictionary, NSNumber等写法的进化的更多相关文章

  1. Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法

    Xcode4.4(LLVM4.0编译器)中NSArray, NSDictionary, NSNumber优化写法 从xcode4.4开始,LLVM4.0编译器为Objective-C添加一些新的特性. ...

  2. [转]一些NSArray,NSDictionary,NSSet相关的算法知识

    iOS编程当中的几个集合类:NSArray,NSDictionary,NSSet以及对应的Mutable版本,应该所有人都用过.只是简单使用的话,相信没人会用错,但要做到高效(时间复杂度)精确(业务准 ...

  3. 理解Objective C 中id

    什么是id,与void *的区别 id在Objective C中是一个类型,一个complier所认可的Objective C类型,跟void *是不一样的,比如一个 id userName, 和vo ...

  4. 浅谈Objective—C中的面向对象特性

    Objective-C世界中的面向对象程序设计 面向对象称程序设计可能是现在最常用的程序设计模式.如何开发实际的程序是存在两个派系的-- 面向对象语言--在过去的几十年中,很多的面向对象语言被发明出来 ...

  5. (转载)OC学习篇之---Foundation框架中的NSArray类和NSMutableArray类

    在之前的一篇文章中介绍了Foundation框架中的NSString类和NSMutableString类,今天我们继续来看一下Foundation框架中的NSArray类和NSMutableArray ...

  6. OC学习篇之---Foundation框架中的NSArray类和NSMutableArray类

    我们继续来看一下Foundation框架中的NSArray类和NSMutableArray类,其实NSArray类和Java中的List差不多,算是一种数据结构,当然我们从这两个类可以看到,NSArr ...

  7. NSData NSDate NSString NSArray NSDictionary 相互转换

    // NSData NSDate NSString NSArray NSDictionary json NSString *string = @"hello word"; NSDa ...

  8. Read and Write NSArray, NSDictionary and NSSet to a File

    查询地址:http://iosdevelopertips.com/data-file-management/read-and-write-nsarray-nsdictionary-and-nsset- ...

  9. objective C中的字符串NSStirng常用操作

    objective C中的字符串操作 在OC中创建字符串时,一般不使用C的方法,因为C将字符串作为字符数组,所以在操作时会有很多不方便的地方,在Cocoa中NSString集成的一些方法,可以很方便的 ...

随机推荐

  1. python-format函数

    #通过位置 print '{0},{1}'.format('chuhao',20) print '{},{}'.format('chuhao',20) print '{1},{0},{1}'.form ...

  2. sql语句之约束条件

    not null约束,需设置默认值 sex enum('male','female') not null default 'male' unique 约束,值唯一 单列唯一: create table ...

  3. Laravel中使用模型对数据进行操作

    public function orm(){ //查询表的所有记录 //$user = Admin::all(); //dd($user); //查询某一条记录 //$user = Admin::fi ...

  4. Makefile研究 (一)—— 必备语法

    摘自:http://blog.csdn.net/jundic/article/details/17535445 参考文档:http://blog.csdn.net/wrx1721267632/arti ...

  5. [开源]OSharpNS - .net core 快速开发框架 - 快速开始

    什么是OSharp OSharpNS全称OSharp Framework with .NetStandard2.0,是一个基于.NetStandard2.0开发的一个.NetCore快速开发框架.这个 ...

  6. 求无向图的割点和桥模板(tarjan)

    一.基本概念 1.桥:若无向连通图的边割集中只有一条边,则称这条边为割边或者桥 (离散书上给出的定义.. 通俗的来说就是无向连通图中的某条边,删除后得到的新图联通分支至少为2(即不连通: 2.割点:若 ...

  7. ubuntu 14.04 源码编译mysql-5.7.17

    环境为 Ubuntu 12.04 64 位的桌面版 编译的mysql 版本为 5.7.18 首先需要安装一下依赖包 sudo apt-get install libncurses5-dev cmake ...

  8. Spring pom.xml配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  9. C【C#公共帮助类】分页逻辑处理类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Comm ...

  10. Influxdb 时序数据库 centos 安装

    Influxdb 环境搭建 操作系统:CentOS 7 X64 SSH工具:PuTTY 操作系统安装,请参照官网文档进行:https://www.centos.org/ 使用PuTTY 通过ssh连接 ...