简体   繁体   中英

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.

您需要将视图中的标签类(故事板或笔尖)更改为ErrorMessageLabel,可在此处找到: http : //prntscr.com/flkor0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM