当我尝试从 Realm 对象读取和写入时出错
Error when I tried to read and write from Realm objects
我是第一次尝试 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 的声明更改为以下内容吗?
class Test : Object
我是第一次尝试 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 的声明更改为以下内容吗?
class Test : Object