openWithCompletionHandler完成处理程序参数始终为“NO”

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完成块的success参数总是等于NO。这是什么原因呢?

2个回答

1
我不能告诉你具体需要做什么,但我可以告诉你如何获取错误信息,以便你能够找到解决方法。
在你的 BCCardDocument 类的 @implementation 部分中,添加类似以下代码的内容:
- (void)handleError:(NSError *)error userInteractionPermitted:(BOOL)userInteractionPermitted {
    NSLog(@"Error: %@ userInfo=%@", error.localizedDescription, error.userInfo);
    [super handleError:error userInteractionPermitted:userInteractionPermitted];
}

0

我有非常类似的代码,它可以正常工作。我假设你的BCCardDocument是UIDocument的一个子类?如果是这样,它需要有这两个方法:

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

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

唯一的另一个区别是我不调用stopQuery。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接