iphone - 为什么在 Objective-C 上保留属性可能不保留?

标签 iphone ios objective-c properties retain

为什么下面的代码会崩溃?注释代码不会崩溃。

@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/

相关文章:

iphone - Objective-C : Call a method from another class in addTarget:action:forControlEvents

iphone - 如何在 iPhone 上以卡拉 OK 风格显示歌词?

iphone - 在 iOS 5 中使用 MPMoviePlayerController 重复视频中的场景的最佳方式?

当指针设置为 nil 时的 Objective-C 引用计数(无 ARC)

ios - 无法识别的选择器发送到 UILabel 上的实例

ios - 无法通过 APNS 获得推送通知

ios - 使您的自定义 View Controller 成为根 IOS

ios - 自动替换 UIView 中的 subview

ios - 实时应用程序版本未反射(reflect)在代码中

objective-c - 何时在 ARC 的对象引用上使用 __block 关键字