领域文件的快速副本-不起作用 [英] Swift Copy of Realm File - Not Working

查看:61
本文介绍了领域文件的快速副本-不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速版本3.

我正在尝试在启动时将我的realm.default文件复制到我的应用程序中(如果该文件不存在),以便可以将其打包为分发的一部分.该文件需要可变,因此我将其复制到文档目录中.

I am working on copying my realm.default file into my application upon start if it doesn't exist so that I can package it as part of distribution. The file needs to be mutable so I am copying it over to the documents directory.

不幸的是,我收到一个错误消息,指出该文件不存在.我已验证路径正确,并且.app中包含文件.

Unfortunately I am getting an error that the file doesn't exist. I have verified that the paths are correct, and that the .app has the file in it.

话虽如此,文件上有一个带圆圈的白色圆圈(请勿输入type),并说当我尝试打开它时无法在这种Mac上打开它.我可以通过选择显示包内容来查看内容,并且文件包含在其中.

With that being said, the file has a white circle with a line on it (do not enter type) and says that I can't open it on this kind of mac when I try to open it. I can see the contents by selecting show package contents and the file is contained within.

上方文字.

以下是我的应用程序委托中的代码,用于加载领域文件:

The following is the code from my App Delegate that loads the realm file:

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

func openRealm() {

    let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL!
    let bundleReamPath = Bundle.main.path(forResource: "default", ofType:"realm")

    if !FileManager.default.fileExists(atPath: String(describing: defaultRealmPath)) {
        do
        {
            try FileManager.default.copyItem(atPath: bundleReamPath!, toPath: String(describing: defaultRealmPath))
        }
        catch let error as NSError {
        // Catch fires here, with an NSError being thrown
        print("error occurred, here are the details:\n \(error)")
        }
    }
}

以下是错误消息(结合上下文):

The following are the error messages (bolded for context):

发生错误,详细信息如下: 错误域= NSCocoaErrorDomain代码= 4文件"default.realm"不存在." UserInfo = {NSSourceFilePathErrorKey = /用户/用户名/库/开发人员/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Bundle/Application/B324E72E-2B5A-4A02-BC2C-FB542FDA6957/AppName .app/default.realm ,NSUserStringVariant =( 复制 ),NSDestinationFilePath = file:///Users/username/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Data/Application/565716DD-E903-409F-B7C8-20154B0DF6BA/Documents/default.realm,NSFilePath =/Users/username/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Bundle/Application/B324E72E-2B5A-4A02-BC2C-FB542FDA6957/AppName. app/default.realm,NSUnderlyingError = 0x618000241bc0 {Error Domain = NSPOSIXErrorDomain Code = 2没有这样的文件或目录"}}

error occurred, here are the details: Error Domain=NSCocoaErrorDomain Code=4 "The file "default.realm" doesn’t exist." UserInfo={NSSourceFilePathErrorKey=/Users/username/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Bundle/Application/B324E72E-2B5A-4A02-BC2C-FB542FDA6957/AppName.app/default.realm, NSUserStringVariant=( Copy ), NSDestinationFilePath=file:///Users/username/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Data/Application/565716DD-E903-409F-B7C8-20154B0DF6BA/Documents/default.realm, NSFilePath=/Users/username/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Bundle/Application/B324E72E-2B5A-4A02-BC2C-FB542FDA6957/AppName.app/default.realm, NSUnderlyingError=0x618000241bc0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

任何人对此都有任何想法.至少到用户/用户名/库/开发人员/CoreSimulator/设备/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Bundle/Application/B324E72E-2B5A-4A02-BC2C-FB542FDA6957的路径/AppName.app/存在,然后将该文件包含在包中.

Anyone have any ideas on this. At a minimum, the path all the way up to Users/username/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Bundle/Application/B324E72E-2B5A-4A02-BC2C-FB542FDA6957/AppName.app/ exists and then the file is contained within the package.

推荐答案

因为您混淆了pathURL. Realm.Configuration.defaultConfiguration.fileURL返回URL的实例. Bundle.main.path()返回String的实例. URL的字符串表示形式不同于path.

Because you're confusing path and URL. Realm.Configuration.defaultConfiguration.fileURL returns instance of URL. Bundle.main.path() returns instance of String. A String representation of URL isn't same as path.

例如

print(String(describing: defaultRealmPath))
// => file:///Users/.../Documents/default.realm

print(defaultRealmPath.path)
// => /Users/.../Documents/default.realm

因此,您应该使用一个(路径或URL).如果使用path,请使用defaultRealmPath.path代替String(describing: defaultRealmPath),如下所示:

So you should use either one (path or URL). If you use path, use defaultRealmPath.path instead String(describing: defaultRealmPath) like the following:

let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL!
let bundleReamPath = Bundle.main.path(forResource: "default", ofType:"realm")

if !FileManager.default.fileExists(atPath: defaultRealmPath.path) {
    do
    {
        try FileManager.default.copyItem(atPath: bundleReamPath!, toPath: defaultRealmPath.path)
    }
    catch let error as NSError {
        // Catch fires here, with an NSError being thrown
        print("error occurred, here are the details:\n \(error)")
    }
}

如果使用URL,则使用Bundle.main.url()代替Bundle main.path(): 让defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL!

If you use URL, Bundle.main.url() instead Bundle main.path(): let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL!

let bundleReamPath = Bundle.main.url(forResource: "default", withExtension:"realm")!

if !FileManager.default.fileExists(atPath: defaultRealmPath.path) {
    do
    {
        try FileManager.default.copyItem(at: bundleReamPath, to: defaultRealmPath)
    }
    catch let error as NSError {
        // Catch fires here, with an NSError being thrown
        print("error occurred, here are the details:\n \(error)")
    }
}

这篇关于领域文件的快速副本-不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆