iphone – 如何在不同对象之间共享一个UIManagedDocument?

iphone – 如何在不同对象之间共享一个UIManagedDocument?,第1张

概述我已经看过 How do I create a global UIManagedDocument instance per document-on-disk shared by my whole application using blocks?,但我真的不明白. 我想要实现的是,整个应用程序应该只有一个UIManagedDocument – 一个核心数据库.不同的对象应该调用一个方法并获得唯一的 我已经看过 How do I create a global UIManagedDocument instance per document-on-disk shared by my whole application using blocks?,但我真的不明白.

我想要实现的是,整个应用程序应该只有一个UIManageddocument – 一个核心数据库.不同的对象应该调用一个方法并获得唯一的UIManageddocument.

我使用一个带有类方法的辅助类:

+ (UIManageddocument *)getsharedDatabase:(Nsstring *)databasename{    NSURL *url = [[[NSfileManager defaultManager] URLsForDirectory:NSdocumentDirectory inDomains:NSUserDomainMask] lastObject];    url = [url URLByAppendingPathComponent:databasename];    // url is Now "<documents Directory>/<databasename>"    if (![[NSfileManager defaultManager] fileExistsAtPath:[url absoluteString]])    {        // does not exist on disk,so create one        UIManageddocument *manageddocument = [[UIManageddocument alloc] initWithfileURL:url];        return manageddocument;    }    else    {        UIManageddocument *manageddocument = **?????**        return manageddocument;    }}

正如您在问号中看到的那样,我不知道如何获取现有文件.我检查了UIManageddocument类引用但找不到它.

你能帮我吗?
提前谢谢了.

编辑
我想知道……单身方法如何:

+ (UIManageddocument *) sharedDatabase{    NSURL *url = [[[NSfileManager defaultManager] URLsForDirectory:NSdocumentDirectory inDomains:NSUserDomainMask] lastObject];    url = [url URLByAppendingPathComponent:@"databasename"];    // url is Now "<documents Directory>/databasename"    static UIManageddocument *manageddocument = nil;    static dispatch_once_t mngddoc;    dispatch_once(&mngddoc,^{            manageddocument = [[UIManageddocument alloc] initWithfileURL:url];    });    return manageddocument;}
解决方法 如果磁盘上存在UIManageddocuments,则需要在目录中搜索匹配的文件.通常,文件将具有一些标识属性,如公共文件扩展名.

像这样的东西:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];NSURL *appDirectoryURL = [appDelegate applicationdocumentsDirectory];NSArray *directoryContents = [[NSfileManager defaultManager] contentsOfDirectoryAtURL:appDirectoryURL includingPropertIEsForKeys:nil options:NSDirectoryEnumerationSkipsHIDdenfiles error:nil];for (NSURL *fileURL in directoryContents) {    Nsstring *documentExtension = [fileURL pathExtension];    if ([documentExtension isEqualToString:@"myfileextension"]) {    }}
总结

以上是内存溢出为你收集整理的iphone – 如何在不同对象之间共享一个UIManagedDocument?全部内容,希望文章能够帮你解决iphone – 如何在不同对象之间共享一个UIManagedDocument?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1081646.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存