gpt4 book ai didi

objective-c - 编码 NSMutableArray

转载 作者:行者123 更新时间:2023-12-03 17:04:05 25 4
gpt4 key购买 nike

编辑:好的,我决定将数组保存在 userDefaults 中......应该很容易,对吧?保存:

NSUserDefaults *userDefs = [NSUserDefaults standardUserDefaults];
[userDefs setObject:videoArray forKey:@"dataArray"];
[userDefs synchronize];

负载:

 NSUserDefaults *userDefs = [NSUserDefaults standardUserDefaults];
videoArray = [[NSUserDefaults standardUserDefaults] mutableArrayValueForKey:@"dataArray"];

[tableview reloadData];
NSLog(@"%@",videoArray);

数组中对象的类:

@interface DEVideoModel : NSObject

@property (copy) NSString *name;
@property (copy) NSImage *thumbnail;
@property (copy) NSDictionary *qualities;
@property (readwrite) float videoSize;
@property (readwrite) float progress;
@property (copy) NSString *filePath;
@property (copy) NSDate *datum;


@end

@synthesize name,filePath,videoSize,qualities,thumbnail,datum,progress;
-(id)init {
self = [super init];
if(self) {
qualities = [[NSDictionary alloc]init];
thumbnail = [[NSImage alloc]init];
}
return self;
}


@end

当我加载它时,我的 videoArray 是(空)?!我不明白。顺便说一下,videoArray 是一个 NSMutableArray,而不是 NSArray。

最佳答案

在您的代码中,您正在将 NSData 写入 NSCoder,因此您需要读取 NSData 然后将其转换为数组。

NSURL *appSupportDir = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:&error];
NSString *path = [NSString stringWithFormat:@"%@/DEConvert.dat",[appSupportDir path]];
NSLog(@"%@",appSupportDir);
NSData *data = [NSData dataWithContentsOfFile:path];
NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithData:data];

将对象存储在 NSUserDefault

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arr];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:@"your key"];

取消存档同样简单:

NSData *NewData = [[NSUserDefaults standardUserDefaults] objectForKey:@"your key"];
NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithData:NewData];

关于objective-c - 编码 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15600942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com