gpt4 book ai didi

swift - 快速创建 Realm 对象时出错

转载 作者:行者123 更新时间:2023-11-28 15:18:42 25 4
gpt4 key购买 nike

import UIKit
import Realm
import RealmSwift

class Employee: Object {
dynamic var name = ""
dynamic var salary = 0
}
class Emp: Object{
dynamic var name = ""
dynamic var salary = ""
}

class RealMEx: UIViewController,UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var txt1: UITextField!
@IBOutlet weak var txt2: UITextField!

@IBOutlet weak var tblView: UITableView!

var dataArry = [[String: Any]]()

@IBAction func Submit(_ sender: UIButton) {
let emp = Employee()
emp.name = txt1.text!
emp.salary = Int(txt2.text!)!

let realm = try! Realm()

try! realm.write {
realm.deleteAll()
realm.add(emp)
}

let data = realm.objects(Employee.self)

for i in Array(data){
let d = ["name":i["name"]!,"salary": i["salary"]!]
dataArry.append(d as [String : Any])
}
print (Array(data))
tblView.reloadData()
}

override func viewDidLoad() {
super.viewDidLoad()
tblView.delegate = self
tblView.dataSource = self
}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArry.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)as! RealMTableCell
cell.lbl.text = dataArry[indexPath.row]["name"] as? String
cell.sallbl.text = dataArry[indexPath.row]["salary"] as? String
return cell
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

我正在尝试创建简单的 Realm 对象来存储和检索数据,所以首先我编写了这段代码,首先我使用了两个不同的数组来存储我的数据,但后来我使用了关联数组。但是使用关联数组我无法在表格 View 中打印我的“薪水”。所以我选择了另一个名为“Emp”的类并运行我的代码,但之后它显示了这个错误。

2017-09-25 10:54:07.218441+0530 NewLogin[2264:51915] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /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] Reading from private effective user settings.

2017-09-25 10:54:18.146 NewLogin[2264:52118] Version 2.10.1 of Realm is now available: https://github.com/realm/realm-cocoa/blob/v2.10.1/CHANGELOG.md

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=10 "Migration is required due to the following errors: - Property 'Employee.salary' has been changed from 'int' to 'string'." UserInfo={NSLocalizedDescription=Migration is required due to the following errors: - Property 'Employee.salary' has been changed from 'int' to 'string'., Error Code=10}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-802.0.53/src/swift/stdlib/public/core/ErrorType.swift, line 182

最佳答案

由于您已经更改了一个 Realm 模型类,因此您需要定义一个迁移 block 并执行迁移。您实际上不需要在迁移 block 中执行任何操作,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
}

代码复制自official documentation Realm 。

关于swift - 快速创建 Realm 对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46398350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com