如何使用 get return 获取 IBInspectable var 值?

How to get IBInspectable var value with get return?

我有简单的评级应用程序,我有;

 @IBInspectable var commingId: Int
        {
        get
        {
            return self.commingId;
        }
        set
        {
            refreshStars()
        }
    }


func starClicked(sender:UIButton){
    if(self.canEdit){
        rating = sender.tag;
        if(self.delegate != nil){
            self.delegate.starRateEdited(Double(rating))
        }

        self.refreshStars()
        print("Selected \(sender.tag) - commingId \(self.commingId)")



    }
}

代码。当我试图得到 return self.commingId 给我时;

EXC_BAD_ACCESS(code=2,address=0x7fff571egff8) error

如何才能将 commingId 放入其中?

尝试以下:

var commingRating: Int = 0

@IBInspectable var commingId: Int
            {
            get
            {
                return self.commingRating;
            }
            set
            {
                commingRating = newValue
                refreshStars()
            }
        }


    func starClicked(sender:UIButton){
        if(self.canEdit){
            rating = sender.tag;
            if(self.delegate != nil){
                self.delegate.starRateEdited(Double(rating))
            }

            self.refreshStars()
            print("Selected \(sender.tag) - commingId \(self.commingId)")



        }
    }

Why do you even redefined getter?

看起来你只需要在设置值后调用refreshStars(),所以只需使用willSet/didSet

@IBInspectable var commingId: Int {
    didSet {
        refreshStars()
    }
}