Search code examples
iosswiftibinspectable

How to get IBInspectable var value with get return?


I have simple rating app and i have;

 @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)")



    }
}

codes. when i try to get return self.commingId gives me;

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

How can i get commingId inside it ?


Solution

  • Why do you even redefined getter?

    It looks like you just need to call refreshStars() after value is set, so just work with willSet/didSet

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