AixNPanes
AixNPanes

Reputation: 1270

Swift 3 How do I resolve EXC_BAD_ACCESS in func

I have the following class:

import UIKit

class ErrorMessageLabel: UILabel {
    open func setErrorText(button:UIButton?, message:String) {
        self.text = message
        self.isHidden = (message == "")
        if (button != nil) {button?.isEnabled = (message == "")}
    }
}

which I use in a View Controller:

class RegistrationViewController: SuperViewController {
    @IBOutlet weak var errorMessageLabel: ErrorMessageLabel!

    @IBOutlet weak var registerButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        checkValidData()
    }

    private func checkValidData()
    {
        errorMessageLabel.setErrorText(registerButton, message: "")
        ...
    }

I get Thread 1: EXC_BAD_ACCESS (code=2, address=...)on the setErrorText call

If i change the ErrorMessageLabel.swift to the following

import UIKit

extension UILabel {
    open func setErrorText(button:UIButton?, message:String) {
        self.text = message
        self.isHidden = (message == "")
        if (button != nil) {button?.isEnabled = (message == "")}
    }
}

class ErrorMessageLabel: UILabel {
}

the code works. Obviously, this is the wrong place to put the setErrorText as the code does not apply to all UILabels. What is the proper fix?

I'm a relative newbie to IOS development.

Upvotes: 2

Views: 105

Answers (1)

Hussein Dimessi
Hussein Dimessi

Reputation: 376

you need to change the class of the label in your view (storyboard or nib) to ErrorMessageLabel you can find it here: http://prntscr.com/flkor0

Upvotes: 4

Related Questions