Swift Copy ของไฟล์ Realm - ไม่ทำงาน

สวิฟท์เวอร์ชั่น 3

ฉันกำลังคัดลอกไฟล์ realm.default ลงในแอปพลิเคชันของฉันตั้งแต่เริ่มต้น หากไม่มีอยู่ เพื่อที่ฉันจะได้จัดทำแพ็กเกจเป็นส่วนหนึ่งของการแจกจ่ายได้ ไฟล์จะต้องไม่แน่นอน ดังนั้นฉันจึงคัดลอกไปยังไดเร็กทอรีเอกสาร

ขออภัย ฉันได้รับข้อผิดพลาดว่าไม่มีไฟล์นี้ ฉันได้ตรวจสอบแล้วว่าเส้นทางนั้นถูกต้อง และ .app มีไฟล์อยู่ในนั้น

ตามที่กล่าวไว้ ไฟล์นั้นมีวงกลมสีขาวมีเส้นอยู่ (อย่าป้อนประเภท) และบอกว่าฉันไม่สามารถเปิดมันบน Mac ประเภทนี้ได้เมื่อฉันพยายามเปิดมัน ฉันสามารถดูเนื้อหาได้โดยเลือกแสดงเนื้อหาแพ็คเกจและมีไฟล์อยู่ภายใน

ข้อความข้างบน..

ต่อไปนี้เป็นโค้ดจาก App Delegate ของฉันที่โหลดไฟล์ realm:

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)")
        }
    }
}

ต่อไปนี้เป็นข้อความแสดงข้อผิดพลาด (เป็นตัวหนาสำหรับบริบท):

เกิดข้อผิดพลาด นี่คือรายละเอียด: Error Domain=NSCocoaErrorDomain Code=4 "ไฟล์ “default.realm” ไม่มีอยู่” UserInfo={NSSourceFilePathErrorKey=/Users/username/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Bundle/Application/B324E72E-2B5A-4A02-BC2C-FB542FDA6957 /ชื่อแอป .app/default.realm, NSUserStringVariant=( คัดลอก), NSDestinationFilePath=file:///Users/username/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/ ข้อมูล/แอปพลิเคชัน/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 {โดเมนข้อผิดพลาด = NSPOSIXErrorDomain Code = 2 "ไม่มีไฟล์หรือไดเรกทอรีดังกล่าว"}}

ใครมีความคิดเกี่ยวกับเรื่องนี้ อย่างน้อยที่สุด เส้นทางไปจนถึง Users/username/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Bundle/Application/B324E72E-2B5A-4A02-BC2C-FB542FDA6957 /AppName.app/ มีอยู่แล้ว จากนั้นไฟล์จะอยู่ภายในแพ็คเกจ


person Apocal    schedule 24.09.2016    source แหล่งที่มา
comment
ใครมีอะไรเกี่ยวกับเรื่องนี้บ้างไหม? น่าเสียดายที่ฉันยังคงติดอยู่   -  person Apocal    schedule 26.09.2016


คำตอบ (1)


เพราะคุณกำลังสับสน path และ URL Realm.Configuration.defaultConfiguration.fileURL ส่งคืนอินสแตนซ์ของ URL Bundle.main.path() ส่งคืนอินสแตนซ์ของ String การแสดงสตริงของ URL ไม่เหมือนกับ path

e.g.

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

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

ดังนั้นคุณควรใช้อย่างใดอย่างหนึ่ง (เส้นทางหรือ URL) หากคุณใช้ path ให้ใช้ defaultRealmPath.path แทน String(describing: defaultRealmPath) ดังต่อไปนี้:

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!

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)")
    }
}
person kishikawa katsumi    schedule 28.09.2016
comment
ขอบคุณมากครับคุณคิชิคาว่า - person Apocal; 29.09.2016