Search code examples
iosswiftparse-platformsubclassing

Mapping parse object doesn't work in callFunctionInBackground


This is what the test cloud code looks like:

Parse.Cloud.define ( "getSpokey" , function (request , response ) {
var Spokey = Parse.Object.extend("Spokey");

var query = new Parse.Query(Spokey);

query.find({
    success: function(spokey){
        response.success(spokey);
    },
    error: function(spokey, error){
        response.error(error);
    }
});
});

When testing this function on a browser, this is the return I get:

{"result":[{"__type":"Object","className":"Spokey","createdAt":"2015-11-22T13:51:40.187Z","name":"kussa","objectId":"niCUOIFx3V","updatedAt":"2015-11-22T13:51:40.187Z"},{"__type":"Object","className":"Spokey","createdAt":"2015-11-22T13:51:44.592Z","name":"test","objectId":"yGm2hg2GRI","updatedAt":"2015-11-22T13:51:44.592Z"}]}

On the iOS side this is the class code in swift

class SPKSpokey : PFObject, PFSubclassing {

var name : String!

override class func initialize() {
    struct Static {
        static var onceToken : dispatch_once_t = 0;
    }
    dispatch_once(&Static.onceToken) {
        self.registerSubclass()
    }
}

static func parseClassName() -> String {

    return "Spokey"
}
}

In the controller I have the following code:

PFCloud.callFunctionInBackground("getSpokey", withParameters: nil) { (result , error) -> Void in

        if let result = result as? [SPKSpokey]  {

            print(result)
            for sp in result {
                print(sp.name) //crashes here
            }
        }
    }

In my app delegate I have:

SPKSpokey.registerSubClass()

The console shows the following when print(result) is executed:

    [<Spokey: 0x7f8d70c7ef60, objectId: niCUOIFx3V, localId: (null)> {
name = kussa;
}, <Spokey: 0x7f8d70c80cf0, objectId: yGm2hg2GRI, localId: (null)> {

        name = test;
    }]

So my question is: How do I access my class properties for example sp.name without using objectForKey's PFObject


Solution

  • Try changing:

    var name : String!
    

    to:

    @NSManaged public var name: String!