Realm - Add file with initial data to project (iOS/Swift)

后端 未结 7 1447
独厮守ぢ
独厮守ぢ 2020-12-23 12:47

I\'m developing an application for iOS using swift and chose Realm as a database solution for it. I wrote default data in AppDelegate using write/add function from realm doc

7条回答
  •  再見小時候
    2020-12-23 13:28

    Swift version 3, courtesy of Kishikawa Katsumi:

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

提交回复
热议问题