1

を使用するUIDocumentと、の完了ハンドラーopenWithCompletionHandler:が呼び出されることはなく、ドキュメントは閉じたままになります。何が間違っているのかわかりません。すべてが正常に見え、私が見つけたサンプルコードと一致します。これが私がしていることです:

+ (NSString *)documentsDirectory {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

+ (NSURL *)documentsURL {
    NSString *documentsDirectory = [XKCDParser documentsDirectory];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"documentModel"];
    return [NSURL fileURLWithPath:filePath];
}

- (void)loadComicsAfterNumber:(NSInteger)comicNumber {
    if (comicNumber < 0) {
        if ([self.delegate respondsToSelector:@selector(errorLoadingComics:)])
            [self.delegate errorLoadingComics:[NSError errorWithDomain:@"Comic number cannot be less than 0" code:2 userInfo:nil]];
        return;
    }

    self.document = [[UIManagedDocument alloc] initWithFileURL:[XKCDParser documentsURL]];
    NSLog(@"document: %@", document);
    if ([[NSFileManager defaultManager] fileExistsAtPath:[[XKCDParser documentsURL] path]]) {
        NSLog(@"file exists at path");

        [document openWithCompletionHandler:^(BOOL success) {
            NSLog(@"document: %@", document);
            if (success)
                NSLog(@"Document opened sucessfully");
            if (!success)
                NSLog(@"Document did not open successfully");
        }];
        NSLog(@"2 document: %@", document);
    }
    else {
        [document saveToURL:[XKCDParser documentsURL] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            if (!success) {
                NSLog(@"Could not create document at path: %@", [[XKCDParser documentsURL] path]);
                if ([self.delegate respondsToSelector:@selector(errorLoadingComics:)])
                    [self.delegate errorLoadingComics:[NSError errorWithDomain:[NSString stringWithFormat:@"Could not create document at path: %@", [[XKCDParser documentsURL] path]] code:4 userInfo:nil]];
            }
            else
                [self downloadComicsAfterNumber:comicNumber];
        }];
    }
    NSLog(@"3 document: %@", document);
}

コンソール出力

2012-08-28 11:59:09.755 XKCD[12194:c07] document: <UIManagedDocument: 0xd2f90f0> fileURL: file://localhost/Users/User/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/CE8A9DC6-FB09-4426-BDCD-863F157FFA04/Documents/documentModel/ documentState: [Closed]
2012-08-28 11:59:09.757 XKCD[12194:c07] file exists at path
2012-08-28 11:59:09.757 XKCD[12194:c07] 2 document: <UIManagedDocument: 0xd2f90f0> fileURL: file://localhost/Users/User/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/CE8A9DC6-FB09-4426-BDCD-863F157FFA04/Documents/documentModel/ documentState: [Closed]
2012-08-28 11:59:09.761 XKCD[12194:c07] 3 document: <UIManagedDocument: 0xd2f90f0> fileURL: file://localhost/Users/User/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/CE8A9DC6-FB09-4426-BDCD-863F157FFA04/Documents/documentModel/ documentState: [Closed]
4

2 に答える 2

0

ファイルに拡張子付きの名前を付けてみてください。私は通常.dbを使用しますが、何でもかまいません。現在、システムはディレクトリを開こうとしていると思っているかもしれませんが、実際にはパッケージ(Core Dataによって作成されたもの)を開こうとしています。

于 2012-08-30T18:13:07.803 に答える
0

「Document is a package or bundle」を .plist (LSTypeIsPackage) で YES に設定することが重要です。これについては、iOS 用ドキュメントベース アプリ プログラミング ガイド の「ドキュメント タイプを宣言する」セクションで説明されています。

于 2014-10-31T18:25:20.633 に答える