我在浏览Xcode 4.4的发行说明时注意到:

LLVM 4.0编译器

Xcode现在包括Apple LLVM编译器4.0版,包括以下新的Objective-C语言功能:

我对这个功能很感兴趣.我并不完全清楚NSString的文字是如何工作的,以及如何在NSArrayNSDictionaryNSNumber上使用它们.

细节是什么?

推荐答案

逐字复制http://cocoaheads.tumblr.com/post/17757846453/objective-c-literals-for-nsdictionary-nsarray-and:

Objective-C literals: one can now create literals for NSArray, NSDictionary, and NSNumber (just like one can create literals for NSString)

NSArray文字

以前:

array = [NSArray arrayWithObjects:a, b, c, nil];

现在:

array = @[ a, b, c ];

字典文本

以前:

dict = [NSDictionary dictionaryWithObjects:@[o1, o2, o3]
                                   forKeys:@[k1, k2, k3]];

现在:

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

NSN数字文字

以前:

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];

现在:

NSNumber *number;
number = @'X';
number = @12345;
number = @12345ul;
number = @12345ll;
number = @123.45f;
number = @123.45;
number = @YES;

[Edit]

zxoq at http://news.ycombinator.com/item?id=3672744增加了更有趣的新订阅.(添加文字):

arr[1]      === [arr objectAtIndex:1]
dict[@"key"] === [dict objectForKey:@"key"]

[Edit 2]

新的ObjC文本在WWDC 2012多次会议中进行了讨论.我故意没有删除文件名和每张幻灯片的时间,这样你可以自己找到它们,如果你愿意的话.它们基本上与本文中所述的内容相同,但也有一些新的内容我将在图片上方提及.

Please note that images are all big. Simply drag them into another tab to view them in their original size

文字&;拳击

[NSNumber numberWithint:42]
[NSNumber numberWithDouble:10.8]
[NSNumber numberWithBool:YES]
[NSNumber numberWithint:6 + x * 2012]

文字&;拳击

@42
@10.8
@YES
@(6 + x * 2012)

Collection 订阅

[NSArray arrayWithObjects: a, b, c, nil]
[array objectAtIndex:i]
[NSDictionary dictionaryWithObjectsAndKeys: v1, k1, v2, k2, nil];
[dictionary valueForKey:k]

Collection 订阅

@[a, b, c]
array[i]
@{k1:v1, k2:v2}
dictionary[k]

@# numbers, @{} dictionaries, @


This part is new. Expression Literals

当你有一个表达式(例如M_PI / 16)时,你应该把它放在括号里.

这种语法适用于数字表达式、布尔、在(C-)字符串中查找索引、布尔值、枚举常量,甚至字符串!

表达式文字

NSNumber *piOverSixteen = [NSNumber numberWithDouble: (M_PI / 16)];

NSNumber *hexDigit = [NSNumber numberWithChar:"0123456789ABCDEF"[i % 16]];

NSNumber *usesScreenFonts = [NSNumber numberWithBool:[NSLayoutManager usesScreenFonts]];

NSNumber *writingDirection = [NSNumber numberWithInt:NSWritingDirectionLeftToRight];

NSNumber *path = [NSString stringWithUTF8String: getenv("PATH")];

表达式文字

NSNumber *piOverSixteen = @( M_PI / 16 );

NSNumber *hexDigit = @( "0123456789ABCDEF"[i % 16] );

NSNumber *usesScreenFonts = @( [NSLayoutManager usesScreenFonts] );

NSNumber *writingDirection = @( NSWritingDirectionLeftToRight );

NSNumber *path = @( getenv("PATH") );

有关字符串以及如何/何时使用此文字语法的详细信息:

盒装字符串表达式

NSString *path = [NSString stringWithUTF8String: getenv("PATH")];
for (NSString *dir in [path componentsSeparatedByString: @":"]) {
    // search for a file in dir...
}

盒装字符串表达式

NSString *path = @( getenv("PATH") );
for (NSString *dir in [path componentsSeparatedByString: @":"]) {
    // search for a file in dir...
}

数组文字的工作原理

数组文字如何工作

// when you write this:
array = @[a, b, c ];

// compiler generates:
id objects[] = { a, b, c };
NSUInteger count = sizeof(objects) / sizeof(id);
array = [NSArray arrayWithObjects:objects count:count];

字典文字的工作原理

字典文字如何工作

// when you write this:
dict = @{k1 : o1, k2 : o2, k3 : o3 };

// compiler generates:
id objects[] = { o1, o2, o3 };
id keys[] = { k1, k2, k3 };
NSUInteger count = sizeof(objects) / sizeof(id);
dict = [NSDictionary dictionaryWithObjects:objects
                                   forKeys:keys
                                     count:count];

更多关于数组订阅的信息

数组订阅

@implementation SongList {
    NSMutableArray *_songs;
}

- (Song *)replaceSong:(Song *)newSong atindex:(NSUinteger)idx {
    Song *oldSong = [_songs objectAtIndex:idx];
    [_songs replaceObjectAtindex:idx withObject:newSong];
    return oldSong;
}

数组订阅

@implementation SongList {
    NSMutableArray *_songs;
}

- (Song *)replaceSong:(Song *)newSong atindex:(NSUinteger)idx {
    Song *oldSong = _songs[idx];
    _songs[idx] = newSong;
    return oldSong;
}    

更多关于订阅字典的信息

字典订阅

@implementation Database {
    NSMutableDictionary *_storage;
}

- (id)replaceObject:(id)newObject forKey:(id <NSCopying>)key {
    id oldObject = [_storage objectForKey:key];
    [_storage setObject:object forKey:key];
    return oldObject;
}

字典订阅

@implementation Database {
    NSMutableDictionary *_storage;
}

- (id)replaceObject:(id)newObject forKey:(id <NSCopying>)key {
    id oldObject = _storage[key];
    _storage[key] = newObject;
    return oldObject;
}

[Edit 3]

Mike Ash对这些新的文字有很好的描述.如果你想了解更多这方面的知识,一定要去check it out.


Objective-c相关问答推荐

如何从设置屏幕打开位置服务屏幕?

使用 Objective-C 在 iOS 9 中将状态栏文本 colored颜色 更改为浅色

将 CSS 插入 UIWebView / WKWebView 中加载的 HTML

iOS是静态框架还是动态框架?

Facebook SDK 登录永远不会在 iOS 9 上回调我的应用程序

在 Interface Builder 中设计 UITableView 的section header

将 UIColor 保存到 NSUserDefaults 并从中加载

command/usr/bin/codedesign 失败,退出代码 1-代码符号错误

如何获取目标名称?

Xcode 在 iOS 8 中的 Main() 中引发异常,并带有“所有异常”断点

CLLocationManager startUpdatingLocation 未调用 locationManager:didUpdateLocations: 或 locationManager:didFailWithError:

有没有办法在用户已经在 iOS 上拒绝相机访问权限后向他们询问?

iOS8 - 约束含糊地暗示高度为零

远程通知 iOS 8

dispatch_semaphore_dispose 上的 EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)

UIRefreshControl - 在 iOS 7 中 pull 刷新

Block 隐式保留'self';明确提及“touch”以表明这是预期行为

关闭一次后如何返回“允许推送通知”对话框?

如何更改 UICollectionView 中的滚动方向?

如何将 UInt32 设置为最大值