gpt4 book ai didi

objective-c - 无法将 NSData 放入 NSMutableArray

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

我有一个对象数组,我想将其保存为文件并重新加载回我的应用程序中。它正在保存文件(里面有一些数据),但我无法将其读回到 NSMutable 数组中。

对象是符合 NSCoding 协议(protocol)的模型:

@implementation myModel

@synthesize name;
@synthesize number;

-(void) encodeWithCoder: (NSCoder *) encoder
{
[encoder encodeObject:name forKey:@"name"];
[encoder encodeInteger:number forKey:@"number"];
}

-(id) initWithCoder: (NSCoder *) decoder
{
name = [decoder decodeObjectForKey:@"name"];
number = [decoder decodeIntegerForKey:@"number"];
return self;
}

@end

所以我创建了这些对象的数组,然后保存它......

- (void) saveMyOptions {

// Figure out where we're going to save the app's data files
NSString *directoryPath = [NSString stringWithFormat:@"%@/Library/Application Support/MyAppDir/", NSHomeDirectory()]; // points to application data folder for user

// Figure out if that directory exists or not
BOOL isDir;

NSFileManager *fileManager = [[NSFileManager alloc] init];

[fileManager fileExistsAtPath:directoryPath isDirectory:&isDir];

// If the directory doesn't exist, create it
if (!isDir)
{
[fileManager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:NULL];
}

// Assemble everything into an array of objects with options
NSMutableArray *savedPreferences = [[NSMutableArray alloc] init];

myModel *saveOptions = nil;

for (int i; i < [otherArray count]; i++)
{
saveOptions = [[myModel alloc] init];

[saveOptions setName:@"Some String"];
[saveOptions setNumber:i];
[savedPreferences addObject:saveOptions];
saveOptions = nil;
}

// Actually save those options into a file
NSData* saveData = [NSKeyedArchiver archivedDataWithRootObject:savedPreferences];

NSString *fileName = [NSString stringWithFormat:@"%@filename.stuff", directoryPath];

NSError *error = nil;

BOOL written = [saveData writeToFile:fileName options:0 error:&error];

if (!written)
{
NSLog(@"Error writing file: %@", [error localizedDescription]);
}

}

所以现在我尝试将该数据加载回数组中。这就是我认为它崩溃的地方......

- (NSMutableArray *) loadOptions {

// Create file manager object
NSFileManager *fileManager = [[NSFileManager alloc] init];

NSData *saveData = nil;

// Find user directory path
NSString *directoryPath = [NSString stringWithFormat:@"%@/Library/Application Support/MyAppDir/", NSHomeDirectory()]; // points to application data folder for user

// Assign file name
NSString *fileName = [NSString stringWithFormat:@"%@filename.stuff", directoryPath];

// Create options array
NSMutableArray *myOptions = nil;

// If the file exists, fill the array with options
if ([fileManager fileExistsAtPath:fileName])
{
saveData = [NSData dataWithContentsOfFile:fileName];
myOptions = [NSKeyedUnarchiver unarchiveObjectWithData:saveData];
}


NSLog(@"%lu", [myOptions count]); // This ALWAYS reports 0!
NSLog(@"%lu", [saveData length]); // This reports a value of 236;

return myOptions;
}

有人可以指出我出错的方向吗?我完全困惑了:-(

提前致谢!

最佳答案

您在 encodeWithCoder:initWithCoder: 方法中缺少 super 调用,但这只是猜测。为什么不使用 NSUserDefaults 来保存首选项?

您可能还想确保保留的对象是使用合成 setter 设置的。

- (void)encodeWithCoder:(NSCoder *)encoder {
[super encodeWithCoder:encoder];
[encoder encodeObject:name forKey:@"name"];
[encoder encodeInteger:number forKey:@"number"];
}

- (id)initWithCoder:(NSCoder *)decoder {
self = [super initWithCoder:decoder];
if (self) {
self.name = [decoder decodeObjectForKey:@"name"];
self.number = [decoder decodeIntegerForKey:@"number"];
}
return self;
}

供您引用,NSKeyedArchiver 还有一种可以直接用于操作文件的方法:

+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path

NSKeyedUnarchiver:

+ (id)unarchiveObjectWithFile:(NSString *)path

关于objective-c - 无法将 NSData 放入 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9715225/

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