swift CBLModel properties always nil

29 views
Skip to first unread message

Nazar Pallaev

unread,
Nov 14, 2014, 4:02:32 AM11/14/14
to mobile-c...@googlegroups.com

// model

class TestModelUser: CBLModel {

    var userName:NSString?

}


// create

        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)

            }

        }


There is some documents, but "user.userName" is always nil, what did I wrong?

Maybe there is another way to save properties of a model?

Jens Alfke

unread,
Nov 14, 2014, 5:29:12 PM11/14/14
to mobile-c...@googlegroups.com

On Nov 14, 2014, at 1:02 AM, Nazar Pallaev <forev...@gmail.com> wrote:

There is some documents, but "user.userName" is always nil, what did I wrong?


Just as with Core Data, you have to declare the property as dynamic so that CBLModel knows it's supposed to be bound to a document property. The Swift syntax for this is @NSManaged.

—Jens
Reply all
Reply to author
Forward
0 new messages