gpt4 book ai didi

ios - 尝试使用 Realm 时应用程序崩溃 SIGABRT

转载 作者:行者123 更新时间:2023-11-29 00:51:56 26 4
gpt4 key购买 nike

我正在尝试使用 Realm 保存一个简单的对象,但应用程序在尝试进行写入事务时一直崩溃,即使它被包裹在 Do Catch block 中也是如此。

let theme = Theme()
theme.name = "Custom Theme"
theme.backgroundColor = backgroundColor
theme.accentColor = accentColor
theme.numberColor = numColor
theme.functionColor = funcColor

// Add to the Realm inside a transaction
do {
try Realm().write {
do {
try Realm().add(theme, update: true)
} catch {
print("Error saving data")
}
}
} catch {
print("Realm.write error")
}

这是“主题”对象

class Theme : Object {
dynamic var name = ""
dynamic var backgroundColor = ""
dynamic var accentColor = ""
dynamic var numberColor = ""
dynamic var functionColor = ""

override static func primaryKey() -> String? {
return "name"
}

这是崩溃的截图 SIGABRT Crash

编辑:上面导致崩溃的代码仅在单击按钮时执行。也没有控制台输出。我正在通过 CocoaPods 引入 Realm 。

最佳答案

啊,这可能与您创建 Realm 实例的方式有关,试试这个:

let realm = try! Realm()

do {
try realm.write {
do {
try realm.add(theme, update: true)
} catch {
print("Error saving data")
}
}
} catch {
print("Realm.write error")
}

不过,通常您不需要将交易包装到 do-catch block 中:

let realm = try! Realm()

try! realm.write {
realm.add(theme, update: true)
}

关于ios - 尝试使用 Realm 时应用程序崩溃 SIGABRT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38032732/

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