Why the following code crashes? Commented code doesn't crash.
@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];
}
So, why retain property may not retain on Objective-C?
When you write @synthesize lastCurrentDate
- you also create variable named ' lastCurrentState
', and when you write lastCurrentDate = [NSDate date];
you directly access this variable. Properties should be accessed via dot: self.lastCurrentDate = ....;
In last xCodes you don't need to write synthesize - it do it automatically, but creates variable named with '_' prefix. It equals to: @synthesize variable = _variable;
Use self.lastCurrentDate = [NSDate date]
. Because when you use self.lastCurrentDate
, it will assign via setter
method. You declare vaiable via retain property, So your setter method will do two operation, assign
and retain
.
因为您直接分配给实例变量,所以没有使用属性访问器方法:
self.lastCurrentDate = [NSDate date];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.