學分爲Apple DTS幫助我理解這堂課。我在這裏分享他們幫助我的一些例子(稍作修改)。
OK,所以基本上它的工作原理是這樣的:子類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 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
將調用這些方法時,需要自動將您需要保存的任何內容保存到AdditionalContent
目錄內的文檔包中。
如果您需要強制保存,只需撥打您UIManagedDocument
實例如下:
[self updateChangeCount:UIDocumentChangeDone];
目前,我沒有使用這個圖像和視頻 - 不過的例子應該給你足夠的走吧。
你有沒有碰運氣?我正在研究同樣的事情。 –