สวิฟท์เวอร์ชั่น 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/ มีอยู่แล้ว จากนั้นไฟล์จะอยู่ภายในแพ็คเกจ