子类 PFObject 并覆盖 init

subclass PFObject and override init

我想创建自定义 PFObject class,但它给了我一个非常混乱的错误 "Property self.photo not initialized at super.init call"

这是我的自定义代码 class :

import Foundation
import Parse

class Wish: PFObject, PFSubclassing {
var id: String?
var name: String?
var descriptionWish: String?
var photo: UIImage
var photoThumbnail: UIImage
var amount: Double?
var amountDonated: Int?

static func parseClassName() -> String {
    return "Wish"
}

override init() {
    super.init()

}



convenience init(id: String?, name: String?, descriptionWish: String?, photo: UIImage, photoThumbnail: UIImage, amount: Double?, amountDonated: Int?) {
    self.init()
    self.id = id
    self.name = name
    self.descriptionWish = descriptionWish
    self.photo = photo
    self.photoThumbnail = photoThumbnail
    self.amount = amount
    self.amountDonated  = amountDonated

}
}

关于如何为此 class 制作自定义初始值设定项的任何想法? 我想这样称呼我的愿望class:

Wish(id: "123", name: "xxx", descriptionWish: "Lorem ipsum", photo: photoImage, photoThumbnail: photoImageThumbnail, amount: 22.20, amountDonated: 10)

并像这样转换 PFObject

let wish = myPfObject as Wish

感谢任何帮助!!!

尝试将您的子类更新为以下内容。请注意您添加到 Wish 子类的 Parse 属性的 NSManaged 关键字以及 super.init()

的删除

您可能还想研究用 Parse 提供的 PFFile 属性替换 UIImage 属性。您可以使用 ParseUI 框架中包含的 PFImageView 轻松存储和加载图像。

import Foundation
import Parse

class Wish: PFObject, PFSubclassing {

    // MARK: - Parse Core Wish Properties

    @NSManaged var id: String?
    @NSManaged var name: String?
    @NSManaged var descriptionWish: String?
    @NSManaged var photo: UIImage
    @NSManaged var photoThumbnail: UIImage
    @NSManaged var amount: Double?
    @NSManaged var amountDonated: Int?

    // MARK: - Lifecycle

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

    static func parseClassName() -> String {
        return "Wish"
    }    

    convenience init(id: String?, name: String?, descriptionWish: String?, photo: UIImage, photoThumbnail: UIImage, amount: Double?, amountDonated: Int?) {
        self.init()
        self.id = id
        self.name = name
        self.descriptionWish = descriptionWish
        self.photo = photo
        self.photoThumbnail = photoThumbnail
        self.amount = amount
        self.amountDonated  = amountDonated
    }
}