Ios – Add a string property to a UIButton in Swift

iosswiftuibutton

How can I associate a string property with a UIButton in Swift? I don't want the string to appear as the button text, simply to be assigned to the button as an identifier or a key. Here is what I have so far:

func createAnswerButtons() {

    var index:Int
    for index = 0; index < self.currentQuestion?.answers.count; index++ {

        // Create an answer button view
        var answer:AnswerButtonView = AnswerButtonView()
        selection.setTranslatesAutoresizingMaskIntoConstraints(false)

        // Place into content view
        self.scrollViewContentView.addSubview(answer)

        // Add a tapped gesture recognizer to the button
        let tapGesture:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("answerTapped:"))
        answer.addGestureRecognizer(tapGesture)

        // Add constraints etc

        // Set the answer button text
        let answerText = self.currentQuestion!.answers[index]
        answer.setAnswerText(answerText)

        // Set the identifier for each answer button
        self.identifier = self.currentQuestion!.answerIdentifier[index]

        // Add to the selection button array
        self.answerButtonArray.append(answer)
}

So I think I need something after

// Set the identifier for each answer
        self.identifier = self.currentQuestion!.answerIdentifier[index]

To assign the identifier to the button.

The reason for this is I'm trying to implement a decision tree logic so that I can keep track of each answer button that is tapped to generate a code string that will correspond to a final result.

Best Solution

Using the Objective-C runtime, we can add properties to classes at runtime:

extension UIButton {
    private struct AssociatedKeys {
        static var DescriptiveName = "nsh_DescriptiveName"
    }

    @IBInspectable var descriptiveName: String? {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.DescriptiveName) as? String
        }
        set {
            if let newValue = newValue {
                objc_setAssociatedObject(
                    self,
                    &AssociatedKeys.DescriptiveName,
                    newValue as NSString?,
                    UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC)
                )
            }
        }
    }
}

Adding @IBInspectable also lets us set the descriptiveName property through Interface Builder.

For more about the Objective-C runtime, I recommend you check out this NSHipster article.