映射解析对象在callFunctionInBackground

时间:2015-11-22 14:51:52

标签: ios swift parse-platform subclassing

这就是测试云代码的样子:

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);
    }
});
});

在浏览器上测试此功能时,这是我得到的回报:

{"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"}]}

在iOS端,这是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"
}
}

在控制器中,我有以下代码:

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
            }
        }
    }

在我的app委托中,我有:

SPKSpokey.registerSubClass()

执行print(result)时,控制台显示以下内容:

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

        name = test;
    }]

所以我的问题是:如何在不使用sp.name的PFObject的情况下访问我的类属性objectForKey

1 个答案:

答案 0 :(得分:2)

尝试更改:

var name : String!

为:

@NSManaged public var name: String!