我遇到了一些錯誤,當我試圖編譯我的境界代碼,這是代碼的第一個版本:境界錯誤:RLMArray和遷移
import RealmSwift
class Test: Object {
dynamic var name = ""
dynamic var dict = Dictionary<String, Int>()
dynamic var owner: TestList?
}
class TestList: Object{
dynamic var name = ""
let tests = List<Test>().filter("ALL Test.dict[hello] != nil")
}
//ViewController
import RealmSwift
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let realm = Realm()
let test1 = Test()
test1.name = "test1"
test1.dict = ["hello": 1]
realm.write{realm.add(test1)}
let test2 = Test()
test2.name = "test2"
test2.dict = ["nihao": 2]
realm.write{realm.add(test2)}
let test3 = Test()
test3.name = "test3"
test3.dict = ["hello": 3]
realm.write{realm.add(test3)}
}
@IBOutlet weak var label: UILabel!
@IBAction func set(sender: UIButton){
let test = Test()
let realm = Realm()
test.name = "not using CoreData"
realm.write{realm.add(test)}
text = "abc"
}
@IBAction func show(sender: UIButton){
let test = Realm().objects(TestList)
var str = ""
println("got to here")
for i in test{
str += " \(i.name)"
}
label.text = str
}
}
在此設置下我得到一個錯誤我日誌,上面寫着: Terminating app due to uncaught exception 'RLMException', reason: 'This method can only be called on RLMArray instances retrieved from an RLMRealm'
當我刪除了我的TestList對象的篩選,錯誤變成了:Terminating app due to uncaught exception 'RLMException', reason: 'Migration is required for object type 'Test' due to the following errors: - Property 'dict' has been added to latest object model. - Property 'owner' has been added to latest object model.'
難道我理解的領域的文檔完全錯了嗎?
非常感謝您的詳細解答!我能夠解決幾乎所有的錯誤,但我不完全理解最後一個錯誤的解決方法,似乎如果我不從一個新的Realm開始,每次調用它時查詢結果都會建立起來。所以我最終每次運行程序時都會得到一組結果。 – Liumx31
是的,這是正確的。如果你沒有從一個全新的Realm開始,那麼你的查詢結果在每次運行後都會包含一個結果,因爲你在視圖控制器的viewDidLoad方法中添加了對象。這些更改**持續**。畢竟,這就是你使用Realm的原因,對吧? ;) – marius