我正在浏览Xcode 4.4的发行说明并注意到这一点:
LLVM 4.0编译器
Xcode现在包含Apple LLVM Compiler 4.0版,包括以下newObjective-C语言功能: [...]
- Objective-C literals:为NSArray,NSDictionary和NSNumber创建文字,与NSString的文字相同
我对这个功能很感兴趣。我并不完全清楚NSString
的文字是如何工作的,以及如何在NSArray
,NSDictionary
和NSNumber
上使用它们。
有什么细节?
答案 0 :(得分:393)
从http://cocoaheads.tumblr.com/post/17757846453/objective-c-literals-for-nsdictionary-nsarray-and逐字复制:
Objective-C文字: 现在可以为NSArray,NSDictionary和NSNumber创建文字(就像可以为NSString创建文字一样)
此前:
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 };
此前:
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;
<强> [编辑] 强>
http://news.ycombinator.com/item?id=3672744的zxoq 添加了更多有趣的新订阅。 (添加文字):
arr[1] === [arr objectAtIndex:1]
dict[@"key"] === [dict objectForKey:@"key"]
[编辑2]
在多个WWDC 2012会话中讨论了新的ObjC文字。我故意没有删除每个幻灯片的文件名和时间,所以如果你愿意,你可以自己找到它们。它们与本文中所述的内容基本相同,但在图像上方还会提到一些新的内容。
请注意图片都很大。只需将它们拖到另一个标签中即可以原始尺寸查看它们
[NSNumber numberWithint:42]
[NSNumber numberWithDouble:10.8]
[NSNumber numberWithBool:YES]
[NSNumber numberWithint:6 + x * 2012]
@42
@10.8
@YES
@(6 + x * 2012)
[NSArray arrayWithObjects: a, b, c, nil]
[array objectAtIndex:i]
[NSDictionary dictionaryWithObjectsAndKeys: v1, k1, v2, k2, nil];
[dictionary valueForKey:k]
@[a, b, c]
array[i]
@{k1:v1, k2:v2}
dictionary[k]
当你有一个表达式(例如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;
}
[编辑3]
Mike Ash有一篇关于这些新文字的精彩文章。如果您想了解有关此内容的更多信息,请务必check it out。
答案 1 :(得分:15)
Objective-C编译器具有NSConstantString
类(即__CFConstantString
类的实例的内存布局的硬编码知识。查看clang源代码中lib/Rewrite/RewriteModernObjC.cpp
中的RewriteObjCStringLiteral
函数。编译器只发出与NSConstantString
类的实例布局匹配的数据。
文字NSArray
和NSDictionary
实例有两种可能性。他们可以做类似于文字字符串的操作 - 在编译器中硬编码实例布局(用于特殊子类)并在该布局中发出数据。或者他们可以让编译器发出只在运行时创建实例的代码。
答案 2 :(得分:1)
1) Xcode 4.4 中提供了NSNumber
,NSDictionary
和NSArray
文字。
2)NSDictionary
和NSArray
下标需要&#34; Xcode 4.4 和 OS X 10.8 或以后的SDK 强>&#34;或&#34; Xcode 4.5 和 iOS 6 或更高版本的SDK &#34;
在我看来,下标需要运行时支持,因此在 iOS6 之前无法工作。
答案 3 :(得分:0)
Apple LLVM Compiler 4.0 添加了对 Objective-C 的字面支持。它从 at sign
@
NSNumber 文字
NSNumber *someBool = [NSNumber numberWithBool:YES];
//BOOL literal
NSNumber *someBool = @YES;
NSNumber *someChar= [NSNumber numberWithChar:'a'];
//character literal
NSNumber *someChar = @'a';
NSNumber *someInt = [NSNumber numberWithInt:1];
NSNumber *someInt = [NSNumber numberWithUnsignedInt:1U];
NSNumber *someInt = [NSNumber numberWithLong:1L];
NSNumber *someInt = [NSNumber numberWithLongLong:1LL];
//integer literal
NSNumber *someInt = @1;
NSNumber *someInt = @1U;
NSNumber *someInt = @1L;
NSNumber *someInt = @1LL;
NSNumber *someFloat = [NSNumber numberWithFloat:3.141592654F];
NSNumber *someFloat = [NSNumber numberWithDouble:3.1415926535];
//float literal
NSNumber *someFloat = @3.141592654F;
NSNumber *someFloat = @3.1415926535;
集合字面量
NSArray *someArray = [NSArray arrayWithObjects: @"A", @"B", @"C", nil];
//array literal
NSArray *someArray = @[ @"A", @"B", @"C" ];
NSDictionary *someDict = [NSDictionary dictionaryWithObjectsAndKeys:
@"key1", @"value1",
@"key1", @"value2",
nil];
//dictionary literal
NSDictionary *someDict = @{ @"Character" : @"Zelda",
@"key1" : @"value2",
@"key2" : @value2 };
集合下标
NSString *var1 = [someArray objectAtIndex:0]; // Returns 'A'
NSString *var2 = [someDict objectForKey:@"key1"]; // Returns 'value1'
//Collection Subscripting
//read
NSString *var1 = someArray[0]; // Returns 'A'
NSString *var2 = someDict[@"key1"]; // Returns 'value1'
//write to mutable collection
someArray[0] = @"AA";
someDict[@"key1"] = @"value11";
Boxed Expressions - 将 C 风格的表达式转换为 Objective-C。适用于数字、枚举、结构
//Syntax @( <expression> )
[NSNumber numberWithInt:(INT_MAX + 1)];
//Boxed Expressions
NSNumber *var = @(INT_MAX + 1);