由NSMutableDictionary发布/保留

时间:2011-04-03 20:12:47

标签: objective-c ios nsdictionary retain

@interface SearchViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> {

    NSMutableDictionary * results;

}

@property (nonatomic, retain) NSMutableDictionary * results;

和@synthesize结果;在.m

此代码会崩溃

NSMutableDictionary * md = [[NSMutableDictionary alloc] init];

results = md;

[md release];

此代码没问题

NSMutableDictionary * md = [[NSMutableDictionary alloc] init];

results = md;

我很清楚指针不会被保留,但我不知道为什么,我做了什么。错吗

1 个答案:

答案 0 :(得分:3)

你应该使用

self.results = md

所以保留了md。

您使用点语法访问属性。通过使用点语法为属性分配内容,可以调用setter来保留值。如果没有点,您只需将可变字典分配给结果,不会调用setter。

您可以阅读properties in Apple's Objective-C Programming Language,在点语法段落的下方进一步查看常规使用