我有一个奇怪的问题:我在“init”方法中加载对象数据。当我尝试在“viewDidLoad”中访问它时,我的应用程序崩溃了。这是代码:
@interface UploadCenterViewController () {
NSMutableArray *videos;
}
@end
@implementation UploadCenterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
if (![self loadVideos]) {
[self saveVideos];
}
}
return self;
}
-(void)saveVideos {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *encodedData = [NSKeyedArchiver archivedDataWithRootObject:videos];
[defaults setObject:encodedData forKey:@"VIDEOS"];
[defaults synchronize];
}
-(bool)loadVideos {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *encodedData = [defaults objectForKey:@"VIDEOS"];
if (encodedData) {
videos = (NSMutableArray *)[NSKeyedUnarchiver unarchiveObjectWithData:encodedData];
NSLog(@"array size: %d", [videos count]);
return true;
} else {
videos = [[NSMutableArray alloc] init];
return false;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%d", [videos count]);
}
加载数据时,“videos”数组包含一个对象。在“viewDidLoad”中访问它时,整个应用程序崩溃。
有人有想法吗?