Getting the URL of a UIManagedDocument



目前,我确定文档目录并检索其中的URL,并保存到数组属性:

// Returns an array of all the Vacations on file.
- (void)findVacationsOnFile
{
    self.vacationURLs = [[NSArray alloc] init];
    // Identify the documents folder URL.
    NSFileManager *fileManager = [[NSFileManager alloc] init];
    NSError *errorForURLs      = nil;
    NSURL *documentsURL        = [fileManager URLForDirectory:NSDocumentDirectory
                                                     inDomain:NSUserDomainMask
                                            appropriateForURL:nil
                                                       create:NO
                                                        error:&errorForURLs];
    if (documentsURL == nil) {
        NSLog(@"Could not access documents directoryn%@", [errorForURLs localizedDescription]);
    } else {
        // Retrieve the vacation stores on file.
        NSArray *keys = [NSArray arrayWithObjects:NSURLLocalizedNameKey, nil];
        self.vacationURLs = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:documentsURL
                                                          includingPropertiesForKeys:keys
                                                                             options:NSDirectoryEnumerationSkipsHiddenFiles
                                                                               error:nil];
    }
}

当我随后想要 UIMangedDocument 的 URL 时,我会引用数组。 但我怀疑UIManagedDocument已经知道它的URL。 我发现最接近这一点的是 persistentStoreName,但这是一种用于设置持久存储名称的方法。 任何指导都值得赞赏。

是的UIManagedDocumentUIDocument的具体子类,必须使用URL初始化。URL 存储在属性 fileURL(继承自 UIDocument)中,可以像这样访问:

NSURL* url = myUIManagedDocument.fileURL;

最新更新