objective-c - 获取 UIManagedDocument 的 URL

标签 objective-c ios core-data 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 directory\n%@", [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;

关于objective-c - 获取 UIManagedDocument 的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11051836/

相关文章:

ios - 检测到信标时打开新 View

ios - 上传过程中Xcode无效签名问题

ios - 在重新启动 Xcode 之前,XCConfig 不会刷新

swift - 单击 UITableView 时没有任何反应(尝试在 View 之间发送数据)

ios - 中央调度、操作队列、异步、 View 将和确实消失

ios - 如何使用 AFNetworking 2.0 将包含字典的数组发送到服务器?

objective-c - Cocoa 应用程序处理标准 HTTP url 方案

ios - NSCalendar 一个月内的最大天数

cocoa - 无法在支持 AppleScript 的 Core-data 应用程序中使用来自“新建”命令的引用

swift - 本地存储数据的最佳方式是什么(IOS - xcode)