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

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

使用 swift 1.2,我无法再使用解析子类检索指针数组并将其向下转换为另一个解析子类。

我总是发现错误:

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

你有想法还是可能会来?

代码:

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

感谢您的宝贵时间

问题出在这部分代码:

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

registerSubclass() 用于 ShotModel 在 registerSubclass() 用于 PicModel 之前被调用。

我已经在 AppDelegate 中解决了这个问题:

PicModel.registerSubclass()
ShotModel.registerSubclass()

AppDelegate:

中注册后,我还必须以某种方式初始化对象
PicModel.registerSubclass()
PicModel()
ShotModel.registerSubclass()
ShotModel()

问题在于ShotModel注册为PicModel之前的子类。反过来,我们可以将 PicModel 初始化称为 ShotModel 的初始化。

这样我们通过解析保留建议的解决方案并确保 类 以正确的顺序注册。

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

我实际上提交了一份 bug against parse for this very same reason and they ended up updating their subclassing 文档,其中包含以下内容:

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.

因此您需要调用 registerSubclass() 方法,或任何其他方法,以便 class 在 Parse 中正确注册。