简体   繁体   English

Swift 3如何在func中解析EXC_BAD_ACCESS

[英]Swift 3 How do I resolve EXC_BAD_ACCESS in func

I have the following class: 我有以下课程:

import UIKit 导入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 我在setErrorText调用中得到Thread 1: EXC_BAD_ACCESS (code=2, address=...)

If i change the ErrorMessageLabel.swift to the following 如果我将ErrorMessageLabel.swift更改为以下内容

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. 显然,这是放置setErrorText的错误位置,因为该代码不适用于所有UILabel。 What is the proper fix? 正确的解决方法是什么?

I'm a relative newbie to IOS development. 我是IOS开发的相对新手。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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