11

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パッケージ内の「additioncontent」ディレクトリに追加コンテンツを保存することに成功した人はいますか?UUIDをファイル名として(正しいファイル拡張子で)使用して、ストレート画像(PNG、JPEGなど)とビデオ(m4vなど)をこのディレクトリに保存し、これらの個々のファイルへの参照をNSStringファイルパスとして自分のファイルパスとして保存したいと考えています。永続ストア。

4

2 に答える 2

12

このクラスを理解するのを手伝ってくれた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 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;
}

UIManagedDocumentAdditionalContentは必要に応じてこれらのメソッドを呼び出し、ディレクトリ内のドキュメント パッケージに保存する必要があるものを自動的に保存します。

強制的に保存する必要がある場合は、UIManagedDocumentインスタンスで次のように呼び出します。

[self updateChangeCount:UIDocumentChangeDone];

現時点では、これを画像やビデオには使用していませんが、この例で十分に理解できるはずです。

于 2012-01-17T21:28:08.283 に答える
3

The documentation for -additionalContentForURL:error: indicates that returning a nil supposed to signal an error.

  A return value of nil indicates an error condition. To avoid generating 
  an exception, you must return a value from this method. If it is not always
  the case that there will be additional content, you should return a sentinel value (for example, an NSNull instance) that you check for in
  writeAdditionalContent:toURL:originalContentsURL:error:.

I override -writeContents:andAttributes:safelyToURL:forSaveOperation:error: for another purpose (doing some stuff on first save of a new document), and calling super invokes the NSException gods because contents value is nil, not an NSDictionary as seemingly expected by UIManagedDocument. Hmm.

The more you know...

P.S. I guess it depends on the time of day with -writeContents:andAttributes:... It once threw an exception complaining about expecting an NSDictionary, but later threw an exception complaining that I didn't pass it an NSData. My eyebrow could not be raised in a more Spock-like fashion than it is right now.

于 2012-02-14T00:14:49.837 に答える