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

后端 未结 7 1444
独厮守ぢ
独厮守ぢ 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:24

    Updating @pteofil's openRealm function for Swift 2.2/Realm 1.0.2:

    func openRealm() {
    
        let defaultURL =  Realm.Configuration.defaultConfiguration.fileURL!        
        let bundleReamPath = NSBundle.mainBundle().URLForResource("default", withExtension: "realm")
    
    
        if !NSFileManager.defaultManager().fileExistsAtPath(defaultURL.path!) {
            do {
            try NSFileManager.defaultManager().copyItemAtURL(bundleReamPath!,  toURL: defaultURL)
             }
            catch {}
            }
    
    }
    

提交回复
热议问题