繁体   English   中英

使用数组指针和swift 1.2进行PFSubclassing - 致命错误:NSArray元素无法匹配Swift数组元素类型

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

使用swift 1.2,我无法再使用parse子类检索一个poiter数组,并使用另一个parse子类向下转换它。

我总是发现错误:

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

问题在于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()
        }
    }

不知何故,我必须在AppDelegate注册后初始化对象:

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

我实际上因为同样的原因提交了一个针对解析的错误 ,他们最终用以下内容更新了他们的子类文档:

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()方法,或者使用Parse正确注册类的任何其他方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM