PFSubclassing with array pointer and swift 1.2 - fatal error: NSArray element failed to match the Swift Array Element type

南楼画角 提交于 2019-12-10 13:22:30

问题


With swift 1.2, I can no longer retrieve an array of poiter with parse subclass and downcasting it with another parse subclass.

I always found the error:

fatal error: NSArray element failed to match the Swift Array Element type

Do you have an idea or it may come?

The code:

import Foundation

class ShotModel : PFObject, PFSubclassing {

    /**
    * MARK: Properties
    */
    @NSManaged var name: String

    @NSManaged var pics: [PicModel]


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

    class func parseClassName() -> String! {
        return "Shot"
    }

}

import Foundation

class PicModel : PFObject, PFSubclassing {

    /**
    * MARK: Properties
    */
    @NSManaged var name: String


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

    class func parseClassName() -> String! {
        return "Pic"
    }

}

// this cause error

var shot: ShotModel = // a shot model get with fetchInBackgroundWithBlock

shot.pics // fatal error: NSArray element failed to match the Swift Array Element type

Thanks for your time


回答1:


The problem come from this part of code :

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

registerSubclass() for ShotModel is called before registerSubclass() for PicModel.

I've resolved with this in AppDelegate :

PicModel.registerSubclass()
ShotModel.registerSubclass()



回答2:


The problem lies to the fact that ShotModel is registered as a subclass before PicModel. To invert that we can call PicModel initialisation the initialisation of ShotModel.

This way we keep the suggested solution by parse and make sure that classes are registered in the correct order.

class ShotModel : PFObject, PFSubclassing {

    /**
    * MARK: Properties
    */
    @NSManaged var name: String

    @NSManaged var pics: [PicModel]


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



回答3:


Somehow I had to also init the object after registering in AppDelegate:

PicModel.registerSubclass()
PicModel()
ShotModel.registerSubclass()
ShotModel()



回答4:


I actually filed a bug against parse for this very same reason and they ended up updating their subclassing documentation with the following:

Please note that the initialize method is not called until the class receives its first message, meaning that you need to call any instance or class method on your subclass before it will be registered with Parse SDK.

So you NEED to call the registerSubclass() method, or any other method for the class to be registered properly with Parse.



来源:https://stackoverflow.com/questions/29029148/pfsubclassing-with-array-pointer-and-swift-1-2-fatal-error-nsarray-element-fa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!