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中后台运行 (2012-04-01)

iOS开发之KVO的使用 (2012-04-10)

iOS开发之使用lipo命令制作模拟器与真机通用静态库 (2012-04-27)

iOS开发之常用控件尺寸大集合 (2012-06-07)

iOS开发之使用PresentModalViewController (2012-07-04)

iOS开发之深入浅出多线程NSThread (2012-07-27)

iOS开发之Cocos2D中CCSprite执行完动作之后调用回调自行销毁 (2012-08-05)

iOS开发之stretchableImageWithLeftCapWidth:topCapHeight函数用法 (2012-10-23)

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

iOS开发之UIWebView自定义网页中的alert和confirm提示框风格 (2013-03-20)

看看他们的脚印

闲言碎语

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

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

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

文章分类

最近文章

I'M BACK

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

生活不容易,何必呢

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

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

最近评论

日志归档

友情链接

其它功能

随机标签