如何将额外的内容保存到我的UIManagedDocument文件包中? [英] How do I save additional content into my UIManagedDocument file packages?

查看:107
本文介绍了如何将额外的内容保存到我的UIManagedDocument文件包中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解读 Apple的文档时遇到很多麻烦 UIManagedDocument ,特别是以下方法:




  • - (id)additionalContentForURL:(NSURL *)absoluteURL error:(NSError **)error

  • - (BOOL)readAdditionalContentFromURL :(NSURL *)absoluteURL error:(NSError **)error

  • - (BOOL)writeAdditionalContent:(id)content toURL: (NSURL *)absoluteURL originalContentsURL:(NSURL *)absoluteOriginalContentsURL error:(NSError **)error



<有没有人成功地管理将额外的内容保存到他们的UIManagedDocument包中的添加内容目录?我想使用UUID作为文件名(具有正确的文件扩展名)将直接图像(PNG,JPEG等)和视频(m4v等)保存到此目录中,并将对这些单独文件的引用存储为 NSString 文件路径。

解决方案

http://developer.apple.com/support/technical/\"> Apple DTS 帮助我理解此类。我分享了他们在这里帮助我们的一些例子(稍作修改)。



OK,所以基本上是这样工作的:subclass UIManagedDocument ,并实现以下方法(其中 extraInfo 属性只是一个在我们的子类上实现的NSDictionary):

   - (BOOL)readAdditionalContentFromURL:(NSURL *)absoluteURL error:(NSError **)error 
{
NSURL * myURL = [absoluteURL URLByAppendingPathComponent :@AdditionalInformation.plist];
self.extraInfo = [NSDictionary dictionaryWithContentsOfURL:myURL];
return YES;
}

- (id)additionalContentForURL:(NSURL *)absoluteURL错误:(NSError **)错误
{
if(!self.extraInfo){
return [NSDictionary dictionaryWithObjectsAndKeys:@Picard,@Captain,[[NSDate date] description],@RightNow,nil]
} else {
NSMutableDictionary * updatedFriendInfo = [self.extraInfo mutableCopy];
[updatedFriendInfo setObject:[[NSDate date] description] forKey:@RightNow];
[updatedFriendInfo setObject:@YESforKey:@Updated];

return updatedFriendInfo; $($)



- (BOOL)writeAdditionalContent:(id)content toURL:(NSURL *)absoluteURL originalContentsURL:(NSURL *)absoluteOriginalContentsURL error:(NSError **)error
{
if(content){
NSURL * myURL = [absoluteURL URLByAppendingPathComponent:@AdditionalInformation.plist];
[(NSDictionary *)content writeToURL:myURL atomically:NO];
}

return YES;
}

UIManagedDocument 这些方法在需要时,自动保存任何你需要保存到文档包里面的 AdditionalContent 目录。



如果您需要强制保存,只需在 UIManagedDocument 实例上调用以下内容:

  [self updateChangeCount:UIDocumentChangeDone]; 

目前,我不是用于图片和视频 - 。


I'm having a lot of trouble deciphering Apple's documentation around UIManagedDocument, specifically the following methods:

  • - (id)additionalContentForURL:(NSURL *)absoluteURL error:(NSError **)error
  • - (BOOL)readAdditionalContentFromURL:(NSURL *)absoluteURL error:(NSError **)error
  • - (BOOL)writeAdditionalContent:(id)content toURL:(NSURL *)absoluteURL originalContentsURL:(NSURL *)absoluteOriginalContentsURL error:(NSError **)error

Has anyone successfully managed to save additional content into the "addition content" directory inside their UIManagedDocument packages? I'm looking to save straight images (PNGs, JPEGs, etc) and videos (m4v, etc) into this directory using UUIDs as the filenames (with the correct file extension), and storing references to these individual files as NSString file paths within my persistent store.

解决方案

Credit goes to Apple DTS for helping me understand this class. I'm sharing some of the example they helped me with here (modified slightly).

OK, so basically it works like this: subclass UIManagedDocument, and implement the following methods (where the extraInfo property is just an NSDictionary implemented on our subclass):

- (BOOL)readAdditionalContentFromURL:(NSURL *)absoluteURL error:(NSError **)error
{
   NSURL *myURL = [absoluteURL URLByAppendingPathComponent:@"AdditionalInformation.plist"];
   self.extraInfo = [NSDictionary dictionaryWithContentsOfURL:myURL];
   return YES;
}

- (id)additionalContentForURL:(NSURL *)absoluteURL error:(NSError **)error
{
   if (!self.extraInfo) {
       return [NSDictionary dictionaryWithObjectsAndKeys: @"Picard", @"Captain", [[NSDate date] description], @"RightNow", nil];
   } else {
       NSMutableDictionary *updatedFriendInfo = [self.extraInfo mutableCopy];
       [updatedFriendInfo setObject:[[NSDate date] description] forKey:@"RightNow"];
       [updatedFriendInfo setObject:@"YES" forKey:@"Updated"];

       return updatedFriendInfo;
   }
}

- (BOOL)writeAdditionalContent:(id)content toURL:(NSURL *)absoluteURL originalContentsURL:(NSURL *)absoluteOriginalContentsURL error:(NSError **)error
{
   if (content) {
       NSURL *myURL = [absoluteURL URLByAppendingPathComponent:@"AdditionalInformation.plist"];
       [(NSDictionary *)content writeToURL:myURL atomically:NO];
   }

   return YES;
}

UIManagedDocument will call these methods when it needs to, automatically saving whatever you need to save to the document package inside an AdditionalContent directory.

If you need to force a save, simply call the following on your UIManagedDocument instance:

[self updateChangeCount:UIDocumentChangeDone];

At present, I'm not using this for images and videos — but the example should give you enough to go off.

这篇关于如何将额外的内容保存到我的UIManagedDocument文件包中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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