1

I wanted to create custom PFObject class, but it gives me a very confusing error "Property self.photo not initialized at super.init call"

here is code of my custom 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

}
}

Any ideas how to make a custom initializer for this class ? I want to call my wish class like this:

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

and cast PFObject like this

let wish = myPfObject as Wish

Any help is appreciated!!!

Ammo
  • 570
  • 1
  • 8
  • 22

1 Answers1

5

Try updating your subclass to the following. Notice the NSManaged keywords for the Parse properties you've added to your Wish subclass and the removal of super.init()

You will probably also want to look into replacing the UIImage properties with the Parse-provided PFFile properties. You can easily store and load images using PFImageView included in the ParseUI framework.

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
    }
}
Russell
  • 3,099
  • 2
  • 14
  • 18