如何通过get return获得IBInspectable var值?

时间:2016-10-13 10:25:43

标签: ios swift ibinspectable

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

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



    }
}

码。当我试图获得回报时self.commingId给我;

  

EXC_BAD_ACCESS(code = 2,address = 0x7fff571egff8)错误

我如何在其中获得commingId

2 个答案:

答案 0 :(得分:1)

Why do you even redefined getter?

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

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

答案 1 :(得分:-1)

尝试以下:

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



        }
    }