将NSArray保存到iCloud并检索 [英] Save a NSArray to iCloud and retrieve it

查看:124
本文介绍了将NSArray保存到iCloud并检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,当前将NSMutableArray保存到iPad上documents目录中的文件中.这是由

I have an app that currently saves a NSMutableArray to a file in the documents directory on the iPad. This is saved by

[residentData writeToFile:filePath atomically:NO];

并被读回;

residentData = [[NSMutableArray alloc] initWithContentsOfFile:filePath];

我需要将此文件写入iCloud,因为数据需要在多个设备上可用.我已经尝试了以下方法在iCloud中创建文件;

I need to write this file to iCloud, as the data needs to be available across several devices. I have tried the following to create the file in iCloud;

NSData *myFileData = [NSKeyedArchiver archivedDataWithRootObject:residentData];
[fileManager createFileAtPath:[icloudURL path] contents:myFileData attributes:nil];

实际上确实在正确的位置创建了一个文件.我正在努力了解如何将此文件读回到我的NSMutableArray(residentData)中.我已经尝试过;

Which does indeed create a file in the correct place. I am struggling though to understand how to read this file back into my NSMutableArray (residentData). I have tried;

residentData = [NSKeyedUnarchiver unarchiveObjectWithData:myFileData];

但是这不能正确解码到数组中.请有人告诉我我的方式的错误-我怀疑应该归咎于NSData对我的根本误解,但是任何指点都会受到欢迎.

But this doesn't decode into the array correctly. Please can someone show me the error of my ways - I suspect a fundamental misunderstanding on my part of NSData is to blame, but any pointers would be welcome.

推荐答案

终于点击了,我的方法是错误的.我不需要使用任何上述内容.一旦我完成了;

It finally clicked, my approach was wrong. I don't need to use any of the above. Once I have done the;

 [fileManager setUbiquitous:YES itemAtURL:currentPlace destinationURL:icloudURL error:&error];

系统会处理其他所有事情-即只需使用

The system takes care of everything else - i.e. just accessing the file normally with

residentData = [[NSMutableArray alloc] initWithContentsOfURL:icloudURL];

已经工作了.

这篇关于将NSArray保存到iCloud并检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆