在swift中创建领域对象时出错

时间:2017-09-25 05:47:06

标签: swift realm

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="container">
  <div class="col-md-5">
    <form role="form">
      <div class="form-group inner-addon left-addon">
        <input class="form-control user border-style" name="username" placeholder="Username" />
        <i class="fa fa-user-o" aria-hidden="true" data-placeholder="Username"></i>
      </div>
      <br>
      <div class="form-group inner-addon left-addon">
        <input class="form-control user border-style" name="username" placeholder="Username" />
        <i class="fa fa-user-o" aria-hidden="true" data-placeholder="Username"></i>
      </div>
    </form>
  </div>
</div>

我试图创建简单的realm对象来存储和检索数据,所以我首先编写了这段代码,首先我使用了两个不同的数组来存储我的数据但后来我为此采用了关联数组。但是使用关联数组我不能打印我的工资&#34;在表视图中。 所以我又选了一个名为&#34; Emp&#34;并运行我的代码,但之后它显示我的错误。

  

2017-09-25 10:54:07.218441 + 0530 NewLogin [2264:51915] [MC]系统组   systemgroup.com.apple.configurationprofiles路径的容器是   /Users/admin/Library/Developer/CoreSimulator/Devices/9F794470-A0F6-4D8F-8A4C-9CBF6852EE71/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles

     

2017-09-25 10:54:07.239924 + 0530 NewLogin [2264:51915] [MC]阅读   私人有效用户设置。

     

2017-09-25 10:54:18.146 NewLogin [2264:52118] Realm版本2.10.1现已推出:https://github.com/realm/realm-cocoa/blob/v2.10.1/CHANGELOG.md

     

致命错误:&#39;尝试!&#39;表达式意外地引发了错误:错误   Domain = io.realm Code = 10&#34;由于以下原因,需要迁移   错误:    - 财产&#39; Employee.salary&#39;已被改变为&#39; int&#39;到&#39;字符串&#39;。&#34; UserInfo = {NSLocalizedDescription =由于需要迁移   以下错误:    - 财产&#39; Employee.salary&#39;已被改变为&#39; int&#39;到&#39;字符串&#39;。,错误代码= 10}:文件   /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-802.0.53/src/swift/stdlib/public/core/ErrorType.swift,   第182行

2 个答案:

答案 0 :(得分:1)

看着:

  

“由于以下错误,需要迁移: - 属性'Employee.salary'已从'int'更改为'string'。”

立即问题似乎是Realm模型自创建Realm文件(保存到磁盘)后发生了变化。您需要执行Migration

此外,似乎有更新版本的Realm可用,您应该考虑更新。

答案 1 :(得分:0)

您需要定义迁移块并执行迁移,因为您已更改了某个Realm模型类。您实际上不需要在迁移块中执行任何操作,Realm可以自行处理迁移,您只需要将架构版本增加1。

AppDelegate.swift中,定义didFinishLaunchingWithOptions函数内的迁移:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    let config = Realm.Configuration(
        // Set the new schema version. This must be greater than the previously used
        // version (if you've never set a schema version before, the version is 0).
        schemaVersion: 1,
        // Set the block which will be called automatically when opening a Realm with
        // a schema version lower than the one set above
        migrationBlock: { migration, oldSchemaVersion in
            // We haven’t migrated anything yet, so oldSchemaVersion == 0
            if (oldSchemaVersion < 1) {
                // Nothing to do!
                // Realm will automatically detect new properties and removed properties
                // And will update the schema on disk automatically
            }
    })

    // Tell Realm to use this new configuration object for the default Realm
    Realm.Configuration.defaultConfiguration = config

    // Now that we've told Realm how to handle the schema change, opening the file
    // will automatically perform the migration
    let realm = try! Realm()
    return true
}

代码是从Realm的official documentation复制的。