【发布时间】:2015-10-28 20:29:08
【问题描述】:
我做一个这样的单例:
+ (CurrentUser *)sharedInstance {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
它有一个属性叫做:
@property (nonatomic, strong) NSData *profilePicData;
我保存这样一张图片:
[[CurrentUser sharedInstance] setProfilePicData:profilePictureData];
我像这样加载图像:
if ([[CurrentUser sharedInstance]profilePicData]) {
self.profileImageView.image = [UIImage imageWithData:[[CurrentUser sharedInstance]profilePicData]];
}
图像显示并且一切正常,但是当我重新启动应用程序并转到包含相同代码的相同视图控制器时,图像不再出现在 UIImageView 中。这让我相信单例不会持续存在。
如何使用单例对象在应用重新启动时保留数据?
【问题讨论】:
标签: ios objective-c singleton