2012

Nov

30

Xcode4.5新语法,碉堡了
文章分类:iOS开发

New Objective-C Literal Syntax for NSArray, NSDictionary

Apple committed a new patch to the llvm project adding support for new Objective-C literalsyntax for NSArray, NSDictionary and NSNumber. These have previously been documented in the Mountain Lion Xcode release notes but that was still under NDA. Now that these features have been committed to llvm I guess we’re allowed to speak about it.

NSNumber literals

NSNumber *n1 = @1000;  // [NSNumber numberWithInt:1000] 
NSNumber *n2 = @3.1415926; // [NSNumber numberWithDouble:3.1415926]
NSNumber *c = @'c'; // [NSNumber numberWithChar:'c']
NSNumber *b = @YES; // [NSNumber numberWithBool:YES]

// uses the usual suffixes for `unsigned` (`u`) and `float` (`f`)
NSNumber *f = @2.5f;
NSNumber *nu = @256u;

NSArray literals

// before
NSArray *words = [NSArray arrayWithObjects:@"list", @"of", @"words", nil];
// after (array with some strings and numbers)
NSArray *words = @[@"list", @"of", @"words", @123, @3.14];

Note that it’s not needed to include the ending nil sentinel anymore using the new syntax.

NSDictionary literals

NSDictionary *d = @{
  @"key": @"value",
  @"name": @"Joris",
  @"n": @1234,
  @3: @"string"
};

As with NSArray no need to include the nil sentinel here either.

本站原创,转载请标明:来自追梦博客(http://www.wuleilei.com/)

本文地址:http://www.wuleilei.com/Blog/240

相关文章

iOS开发之获取本机IP地址的方法 (2012-01-05)

iOS开发之回调delegate的方法时判断delegate是否已经被释放 (2012-01-31)

iOS开发之使用AES加密(兼容Objective-C与C#) (2012-01-30)

error: C compiler cannot create executables错误的解决方法 (2012-03-01)

iOS开发之使用pjsip开发VoIP应用程序 (2012-03-08)

iOS开发之对象序列化和反序列化 (2012-03-21)

iOS中后台运行 (2012-04-01)

iOS开发之使用UIGestureRecognizer (2012-05-02)

iOS开发之self.abc = nil与[_abc release]的区别 (2012-09-20)

iOS开发之App自定义URL Scheme (2012-10-24)

看看他们的脚印

闲言碎语

突然发现,又是一年 (6月6日)

很久没来看博客了,发现头像又不能显示。。。 (3月28日)

FedEx真是龟速! (8月27日)

文章分类

最近文章

I'M BACK

让iOS App通过iTunes进行文...

生活不容易,何必呢

清除Linux系统中/var/spoo...

iOS开发之UIWebView自定义网...

最近评论

日志归档

友情链接

其它功能

随机标签