为什么下面的代码会崩溃?注释代码不会崩溃。
@property (retain) NSDate *lastCurrentDate;
...
@synthesize lastCurrentDate;
- (void)viewWillAppear:(BOOL)animated {
BOOL crash = [lastCurrentDate isEqualToDate:[NSDate date]]);
}
- (void)viewDidDisappear:(BOOL)animated {
//lastCurrentDate = [[NSDate date] retain];
lastCurrentDate = [NSDate date];
}
那么,为什么在 Objective-C 上 retain 属性可能不 retain?
最佳答案
当您编写 @synthesize lastCurrentDate
- 您还创建名为“lastCurrentState
”的变量,并且当您编写 lastCurrentDate = [NSDate date];
你直接访问这个变量。应通过点访问属性:self.lastCurrentDate = ....;
在最后的 xCodes 中,您不需要编写合成 - 它会自动执行,但会创建以“_”前缀命名的变量。它等于:@synthesize variable = _variable;
关于iphone - 为什么在 Objective-C 上保留属性可能不保留?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16105042/