How to set an initial value for @NSManaged property PFObject Subclass?

懵懂的女人 提交于 2020-01-02 05:42:25

问题


I have a subclass of PFObject called Attendee. In this class, there is a instance variable I have called isFavorite. Below is its class definition:

@NSManaged var isFavorite: Bool

This is an instance var that is local to the device and I never sync it up to the server. In addition, I never explicitly instantiate the Attendee class, but rather create it by typecasting from PFObject. I would like to set the above var to have an initial value of false. How would I achieve this?


回答1:


var isFavorite: Bool {
    get {
        if let isFavorite = self["isFavorite"] as? Bool {
            return isFavorite
        }
        return false //default
    }
    set {
        self["isFavorite"] = newValue
    }
}


来源:https://stackoverflow.com/questions/29980151/how-to-set-an-initial-value-for-nsmanaged-property-pfobject-subclass

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