从http://cocoaheads.tumblr.com/post/17757846453/objective-c-literals-for-nsdictionary-nsarray-和复制的逐字记录:
Objective-C文字:现在可以为NSArray,NSDictionary和NSNumber创建文字(就像可以为NSString创建文字一样)
NSArray文字
先前:
array = [NSArray arrayWithObjects:a, b, c, nil];
现在:
array = @[ a, b, c ];
NSDictionary文字
先前:
dict = [NSDictionary dictionaryWithObjects:@[o1, o2, o3]
forKeys:@[k1, k2, k3]];
现在:
dict = @{ k1 : o1, k2 : o2, k3 : o3 };
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];
现在:
NSNumber *number;
number = @'X';
number = @12345;
number = @12345ul;
number = @12345ll;
number = @123.45f;
number = @123.45;
number = @YES;
[编辑]
zxoq在http://news.ycombinator.com/item?id=3672744增添了更多有趣的新的下标。(添加文字):
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)有很多关于这些新文字的文章。如果您想进一步了解这些东西,请确保将其签出。