3

私はそのようなコードを持っています(私はクラウドからドキュメントを開こうとします):

NSPredicate *pred = [NSPredicate predicateWithFormat:@"%K ENDSWITH '.card'", NSMetadataItemFSNameKey];

NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
[query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
[query setPredicate:pred];

[[NSNotificationCenter defaultCenter] 
 addObserver:self 
 selector:@selector(queryDidFinishGathering:) 
 name:NSMetadataQueryDidFinishGatheringNotification 
 object:query];

[[NSNotificationCenter defaultCenter] 
 addObserver:self 
 selector:@selector(queryDidStartGathering:) 
 name:NSMetadataQueryDidStartGatheringNotification 
 object:query];

[[NSNotificationCenter defaultCenter] 
 addObserver:self 
 selector:@selector(queryDidUpdate:) 
 name:NSMetadataQueryDidUpdateNotification
 object:query];

[query startQuery];

// =========================

- (void)queryDidFinishGathering:(NSNotification *)notification {

    NSMetadataQuery *query = [notification object];
    [query disableUpdates];
    [query stopQuery];

    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:NSMetadataQueryDidFinishGatheringNotification
                                                  object:query];

    for (NSMetadataItem* item in [query results]) {
        NSURL *url = [item valueForAttribute:NSMetadataItemURLKey];
        BCCardDocument *doc = [[[BCCardDocument alloc] initWithFileURL:url] autorelease];
        [doc openWithCompletionHandler:^(BOOL success) {
            if (success) {
                NSLog(@"%@", doc.card.number);
            }
        }];

    }

}

ただし、openWithCompletionHandler完了ブロックの成功引数は常にNOに等しくなります。その理由は何でしょうか?

4

2 に答える 2

1

私はあなたが何をする必要があるかを正確にあなたに言うことはできませんが、あなたがそれを理解することができるように私はあなたにエラーメッセージを得る方法をあなたに教えることができます。

BCCardDocumentクラスの@implementationセクションに、次のようなものを追加します。

- (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted {
    NSLog(@"Error: %@ userInfo=%@", error.localizedDescription, error.userInfo);
    [super handleError:error userInteractionPermitted:userInteractionPermitted];
}
于 2012-05-16T02:39:53.480 に答える
0

私はこれに非常によく似たコードを持っていて、それはうまく機能します。あなたのBCCardDocumentはUIDocumentのサブクラスだと思いますか?その場合、次の2つの方法が必要です。

- (id)contentsForType:(NSString *)typeName error:(NSError **)outError

- (BOOL)loadFromContents:(id)contents
              ofType:(NSString *)typeName
               error:(NSError *__autoreleasing *)outError {

他の唯一の違いは、stopQueryを呼び出さないことです。

于 2012-05-16T02:25:57.097 に答える