我可以这样做吗?尝试从对象内部加载对象

发布于 2024-09-01 18:18:44 字数 1312 浏览 6 评论 0原文

我有一个符合 NSCoding 协议的对象。

该对象包含一个将自身保存到内存的方法,如下所示:

- (void)saveToFile {
 NSMutableData *data = [[NSMutableData alloc] init];
 NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
 [archiver encodeObject:self forKey:kDataKey];
 [archiver finishEncoding];
 [data writeToFile:[self dataFilePath] atomically:YES];
 [archiver release];
 [data release];
}

这工作得很好。但我还想初始化该对象的空版本,然后调用其自己的“Load”方法将文件中存在的任何数据加载到其变量中。所以我创建了以下内容:

- (void)loadFromFile {
 NSString *filePath = [self dataFilePath];
 if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
  NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
  NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  self = [unarchiver decodeObjectForKey:kDataKey];
  [unarchiver finishDecoding];
 } 
}

现在,第二种方法无法加载任何变量。这条线也许不可能吗?

self = [unarchiver decodeObjectForKey:kDataKey];

最终我想使用这样的代码:

一个viewController接受用户输入的输入,创建一个对象并将其保存到内存中使用

[anObject saveToFile];

第二个viewController创建一个空对象,然后通过调用将其值初始化为存储在文件中的值:

[emptyObject loadFromFile];

任何建议关于如何完成这项工作将不胜感激。谢谢:)

迈克尔

I have an object which conforms to the NSCoding protocol.

The object contains a method to save itself to memory, like so:

- (void)saveToFile {
 NSMutableData *data = [[NSMutableData alloc] init];
 NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
 [archiver encodeObject:self forKey:kDataKey];
 [archiver finishEncoding];
 [data writeToFile:[self dataFilePath] atomically:YES];
 [archiver release];
 [data release];
}

This works just fine. But I would also like to initialise an empty version of this object and then call its own 'Load' method to load whatever data exists on file into its variables. So I've created the following:

- (void)loadFromFile {
 NSString *filePath = [self dataFilePath];
 if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
  NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
  NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  self = [unarchiver decodeObjectForKey:kDataKey];
  [unarchiver finishDecoding];
 } 
}

Now this second method doesn't manage to load any variables. Is this line not possible perhaps?

self = [unarchiver decodeObjectForKey:kDataKey];

Ultimately I would like to use the code like this:

One viewController takes user entered input, creates an object and saves it to memory using

[anObject saveToFile];

And a second viewController creates an empty object, then initialises its values to those stored on file by calling:

[emptyObject loadFromFile];

Any suggestions on how to make this work would be hugely appreciated. Thanks :)

Michael

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝梦月影 2024-09-08 18:18:44

为什么不使用 initWithCoder: 来代替呢?它应该返回一个使用给定编码器初始化的对象。由于您的对象符合 NSCoding,因此这不应该是问题。

Why don't you use initWithCoder: instead? It should return you an object initialized with a given coder. Since your object conforms to NSCoding, this shouldn't be an issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文