gpt4 book ai didi

ios - 当我尝试从 Realm 对象读取和写入时出错

转载 作者:行者123 更新时间:2023-11-29 12:18:00 25 4
gpt4 key购买 nike

我是第一次尝试 Realm,但是当我尝试从 Realm 读取和写入时出现错误,这里是带有注释错误行的代码:

class Test{

dynamic var name = ""
}

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

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

@IBOutlet weak var label: UILabel!

@IBAction func show(sender: UIButton){

let test = Realm().objects(Test) //cannot invoke 'objects' with no arguments
label.text = test.name

}


@IBAction func set(sender: UIButton){
let test = Test()
let realm = Realm()
test.name = "not using CoreData"
realm.write{realm.add(test)} //cannot invoke write with argument list of type (()->_)
}


}

如果有人感兴趣,这里是文档页面:https://realm.io/docs/swift/latest/

最佳答案

您可以尝试将 Test 类的声明更改为以下内容吗?

class Test : Object

关于ios - 当我尝试从 Realm 对象读取和写入时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31524206/

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