我尝试创建CBLModel对象并将其保存在couchbase lite中。保存文档时没有错误,但是从数据库中检索文档时,文档的属性始终为零。
// model
class TestModelUser: CBLModel {
var userName:NSString?
}
//创建并保存
let userModel:TestModelUser = TestModelUser(newDocumentInDatabase: db)
userModel.userName = "test user name 10"
userModel.setValue(NSStringFromClass(TestModelUser), ofProperty: "type")
let saved = userModel.save(&error)
if saved {
println("saved")
}else{
if let err:NSError = error {
println(err.localizedDescription)
}
}
// retrive
let dbView:CBLView = db.viewNamed("userView")
dbView.setMapBlock({ (document, emit) -> Void in
emit(document["_id"], document)
if let docType = document["type"] as? String {
if docType == NSStringFromClass(TestModelUser) {
emit(document["_id"], document)
}
}
}, version: "5")
let queryResult1:CBLQueryEnumerator = db.viewNamed("userView").createQuery().run(&error)
for row:CBLQueryRow in queryResult1.allObjects as [CBLQueryRow] {
let user:TestModelUser = TestModelUser(forDocument: row.document)
if let uName = user.userName {
println(uName)
}
}
并且user.userName总是为nil,也许还有另一种保存文档的方法?
答案 0 :(得分:3)
解决。
Document属性应该有@NSManaged修饰符:
class UserModel: CBLModel {
@NSManaged var userName:NSString?
}
感谢jens(https://forums.couchbase.com/t/swift-couchebase-lite-save-cblmodel-document-problem/2054)。