RealmSwiftにカラムを追加してマイグレーションしたいです。
公式ドキュメントを見様見真似でやるものうまくいかず・・・
どこのファイルに公式のコードを添付してマイグレーションするのか知りたいです。
・Realm公式ドキュメンタリー
https://realm.io/docs/swift/latest/#migrations
//旧モデル import Foundation import RealmSwift class Obj: Object{ @objc dynamic var id = 0 @objc dynamic var name: String? override static func primaryKey() -> String? { return "id" } }
import Foundation import RealmSwift class Obj: Object{ @objc dynamic var id = 0 @objc dynamic var name: String? @objc dynamic var profile: String? // プロフィールカラム追加 override static func primaryKey() -> String? { return "id" } }
AppDelegate
1import UIKit 2 3@UIApplicationMain 4class AppDelegate: UIResponder, UIApplicationDelegate { 5 6 7 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 8 // Override point for customization after application launch. 9 10 let config = Realm.Configuration( 11 // Set the new schema version. This must be greater than the previously used 12 // version (if you've never set a schema version before, the version is 0). 13 schemaVersion: 1, 14 15 // Set the block which will be called automatically when opening a Realm with 16 // a schema version lower than the one set above 17 migrationBlock: { migration, oldSchemaVersion in 18 // We haven’t migrated anything yet, so oldSchemaVersion == 0 19 if (oldSchemaVersion < 1) { 20 // Nothing to do! 21 // Realm will automatically detect new properties and removed properties 22 // And will update the schema on disk automatically 23 } 24 }) 25 26 // Tell Realm to use this new configuration object for the default Realm 27 Realm.Configuration.defaultConfiguration = config 28 29 // Now that we've told Realm how to handle the schema change, opening the file 30 // will automatically perform the migration 31 let realm = try! Realm() 32 33 34 return true 35 36 } 37
回答1件
あなたの回答
tips
プレビュー