iCloud - 如何保存包含自定义对象数组的存档

发布于 2024-12-20 14:58:01 字数 932 浏览 3 评论 0原文

我开发了一个小型应用程序,通过存档自定义对象数组在本地存储,其中包含:

@property (nonatomic, copy) NSString    *name;
@property (nonatomic, copy) NSString    *dateCreated;
@property (nonatomic, copy) NSString    *desc;
@property (nonatomic, copy) NSString    *url;

我想使用 iCloud 同步所述存档,并且我相信推荐的机制是通过 UIDocument 子类。 我发现的所有 UIDocument 示例都使用了带有 1 个 NSString 的单个实例,因此我有点困惑如何同步整个自定义对象数组但使用 UIDocument(就像我今天通过 NSCoding 在本地所做的那样)。

我应该创建一个包含上面列出的属性的 UIDocument 对象数组吗?我应该创建一个包含 1 个上述数据对象实例的 UIDocument 实例,然后创建一个包含所有实例的数组,还是应该让 1 个 UIDocument 包含完整的数组自定义对象?

我做了一些研究,但我仍然很困惑。 最后,我只需要同步 1 个包含所述自定义对象数组的文件。

预先感谢您的帮助

我今天拥有的是一个如上所述的自定义类,其中有 4 个名为 Snippet 的字符串,在我的根视图控制器中,我有一个名为 list 的 NSMutableArray,我在其中添加该 Snippet 类的每个新实例。

self.list = [[NSMutableArray alloc] init];
Snippet *newEntry = [[Snippet alloc] init];
[self.list addObject:newEntry];

我应该创建一个拥有自定义对象数组的 UI Document 子类吗?

I have developed a small app that stores locally in iOS through archiving an array of custom objects containing:

@property (nonatomic, copy) NSString    *name;
@property (nonatomic, copy) NSString    *dateCreated;
@property (nonatomic, copy) NSString    *desc;
@property (nonatomic, copy) NSString    *url;

I want to sync said archive using iCloud and I believe the recommended mechanism is through a UIDocument subclass.
All UIDocument examples I found utlilized a single instance with 1 single NSString, so I am a little confused how to go about syncing a whole array of custom objects but utilizing UIDocument (like I do today locally through NSCoding).

Should I create an array of UIDocument objects containing the properties listed above, should I create an instance of UIDocument containing 1 instance of the data object described above and then create an array containing all the instances, or should 1 single UIDocument contain the complete array of custom objects ?

I have done some research but I am still confused.
In the end I would need to sync just 1 file containing an array of said custom objects.

Thanks in advance for your help

What I have today is a custom class as described above with 4 strings called Snippet and in my Root view Controller I have an NSMutableArray called list where I add each new instance of that Snippet Class.

self.list = [[NSMutableArray alloc] init];
Snippet *newEntry = [[Snippet alloc] init];
[self.list addObject:newEntry];

Should I create an UI Document subclass that owns the array of custom objects ?

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

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

发布评论

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

评论(1

韶华倾负 2024-12-27 14:58:01

文档中的示例确实显示了一个只有一个字符串的 UIDocument 子类,但它返回来自 -contentsForType:error: 的 NSData。您可以使用 NSKeyedArchiver 在 NSData 中存储任意数量的对象。阅读序列化对象了解如何操作使用 NSKeyedArchiver 对对象进行编码(并继续阅读以了解如何取回它们!)。

以您的属性为例...

@interface MyDocument : UIDocument
{
}
@property (nonatomic, copy) NSString    *name;
@property (nonatomic, copy) NSString    *dateCreated;
@property (nonatomic, copy) NSString    *desc;
@property (nonatomic, copy) NSString    *url;
@end

@implementation MyDocument

//...

- (id)contentsForType:(NSString *)typeName error:(NSError **)outError
{
    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:name forKey:@"name"];
    [archiver encodeObject:dateCreated forKey:@"created"];
    [archiver encodeObject:desc forKey:@"desc"];
    [archiver encodeObject:url forKey:@"url"];
    [archiver finishEncoding];
    // release archiver if you're not using ARC
    return data;
}
@end;

警告:我尚未编译上面的代码,因此无法保证。这应该作为示例来说明如何使用归档器将多个对象存储在单个数据对象中,您可以将其作为文档内容返回。

The example in the docs does indeed show a UIDocument subclass that just has one string, but it returns a NSData from -contentsForType:error:. You can store as many objects as you like in an NSData using an NSKeyedArchiver. Read Serializing Objects to learn how to encode objects using NSKeyedArchiver (and keep reading to learn how to get them back!).

Using your properties as an example...

@interface MyDocument : UIDocument
{
}
@property (nonatomic, copy) NSString    *name;
@property (nonatomic, copy) NSString    *dateCreated;
@property (nonatomic, copy) NSString    *desc;
@property (nonatomic, copy) NSString    *url;
@end

@implementation MyDocument

//...

- (id)contentsForType:(NSString *)typeName error:(NSError **)outError
{
    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:name forKey:@"name"];
    [archiver encodeObject:dateCreated forKey:@"created"];
    [archiver encodeObject:desc forKey:@"desc"];
    [archiver encodeObject:url forKey:@"url"];
    [archiver finishEncoding];
    // release archiver if you're not using ARC
    return data;
}
@end;

WARNING: I haven't compiled the code above, so no guarantees. This should serve as an example to illustrate using an archiver to store multiple objects in a single data object which you can return as your document's content.

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