NSManaged属性PFObject子类设置初始值

NSManaged属性PFObject子类设置初始值

本文介绍了如何为@NSManaged属性PFObject子类设置初始值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为AttendeePFObject子类.在此类中,有一个我称为isFavorite的实例变量.下面是它的类定义:

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

这是设备本地的实例变量,我从未将其同步到服务器.另外,我从不显式实例化Attendee类,而是通过从PFObject进行类型转换来创建它.我想将上面的var设置为具有false的初始值.我将如何实现呢?

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?

推荐答案

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

这篇关于如何为@NSManaged属性PFObject子类设置初始值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:44