转自 http://www.cocoachina.com/bbs/read.php?tid=107251&fpage=2
@synthesize 可以不用再写了
如果在.h文件里有
|
1
|
@propery NSObject * aProperty
|
那么可以认为 编译器会类似在.m文件里生成
|
1
|
@synthesize aProperty = _aProperty
|
如果上面的都不认识 就可以认为 以下代码
|
1
2
3
4
5
6
7
8
9
|
@interface Spouce:NSObject
@property (strong) NSObject * child
@end@implement Spouce
@end |
和下面的代码没有太大区别(如果在ARC时代写retain、release。。。。)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@interface Spouce:NSObject{
NSObject * _child;
}- (NSObject *) child;
- (void) setChild:(NSObject *) aChild;
@end@implement Spouce
- (NSObject *) child{
return _child;
}- (void) setChild:(NSObject *) aChild{
[aChild retain];
[_child release];
_child = aChild;
}@end |
@literals
@除了可以表示NSString对象外。现在还可以表示数字、数组、字典、和表达式
|
1
2
3
4
5
6
7
8
|
NSString * string = @"a string object";
NSNumber * numberFromNumber= @12;
NSNumber * numberFromExpression= @(20 + 40);
NSArray * array = @[obj1, obj2];
//注意上面不再需要nil结尾NSDictionary * dictionary = @{@"key1" : value1, @"key2" : value2};
//上面也不再需要了,而且key在value前面了。。 |
然后就是这些东西可以互相嵌套,比如
|
1
|
NSArray * array = @[@{@"name" : @"Bacon", @"age" : @12}, @{@"name" : @"Dave", @"age" : @14}];
|
NSArray 和 NSDictionary 可以使用[]语法了
现在可以直接写:
|
1
2
|
id firstElement = anArray[0];
anArray[0] = newValue; |
替代以前的
|
1
2
|
id firstElement = [anArray objectAtIndex:0];
[anArray replaceObjectAtIndex:0 withObject:newValue]; |
用
|
1
2
|
id value = aDictionary[@"key"];
aDictionary[@"key"] = newValue;
|
替代以前的
|
1
2
|
id value = [aDictionary objectForkey:@"key"];
[aDictionary setObject:newValue forKey:@"key"];
|
在.m自己用的“私有”消息可以不用ClassExtension表达了。
想在.m文件里添加自己的一些消息(方法、函数 同义)而不在.h文件里出现,可以在最近的ClassExtension语法里表达如下
|
1
2
3
4
5
6
7
8
9
10
11
12
|
@interface AClass ()
- (void) privateMethod;
@end@implement AClass
- (void) privateMethod{
}@end |
现在 可以直接在.m文件里写,而编译器不会出现警告此方法未声明
|
1
2
3
4
5
6
|
@implement AClass
- (void) privateMethod{
}@end |
如果不知道ClassExtension语法的,
我只想说,ClassExtension是类似Category语法的东西,在.m文件内添加一个无名的Category的@interface声明,然后就可以在里面写私有方法声明,避免编译器乱提示警告。
如果不知道Category语法。。。。。。。。。。。。。。。。