开关允许两个相同的情况?

时间:2019-05-23 10:12:05

标签: ios swift xcode

TestView:

class TestView: UIView{

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }

    func commonInit(){
        addSubview(stackView)
        setConstraints()
    }
    func setConstraints() {
        let constrains = [
            stackView.topAnchor.constraint(equalTo: topAnchor),
            stackView.leadingAnchor.constraint(equalTo: leadingAnchor),
            stackView.bottomAnchor.constraint(equalTo: bottomAnchor),
            stackView.trailingAnchor.constraint(equalTo: trailingAnchor)
        ]

        NSLayoutConstraint.activate(constrains)

    }

    public let accept: UIButton = {
        let v = UIButton()
        v.backgroundColor = .blue
        v.setTitle("Hello", for: .normal)
        v.setTitleColor(.white, for: .normal)
        v.setTitleColor(UIColor.white.withAlphaComponent(0.5), for: [.highlighted,.selected])
        return v
    }()


    public let deny: UIButton = {
        let v = UIButton()
        v.backgroundColor = .red
        v.setTitle("Deny", for: .normal)
        v.setTitleColor(.white, for: .normal)
        v.setTitleColor(UIColor.white.withAlphaComponent(0.5), for: [.highlighted,.selected])
        return v
    }()

    public lazy var stackView: UIStackView = {
        let v = UIStackView(arrangedSubviews: [accept,deny])
        v.alignment = .fill
        v.axis = .vertical
        v.spacing = 8
        v.distribution = .fillEqually
        v.translatesAutoresizingMaskIntoConstraints = false
        return v
    }()
}

TestViewController

class TestViewController: UIViewController{
    var myView: TestView{return view as! TestView}
    unowned var accept: UIButton {return myView.accept}
    unowned var deny: UIButton {return myView.deny}
    override func loadView() {
        view = TestView()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        accept.addTarget(self, action: #selector(didSelect), for: .touchUpInside)
        deny.addTarget(self, action: #selector(didSelect), for: .touchUpInside)
    }

    @objc func didSelect(_ sender: UIButton){
        switch sender{
        case accept:
            print("Accept button clicked")
        case deny:
            print("Deny button clicked")
        case accept:
            print("Accept button case again")
        default:
            break
        }
    }
}

上面的代码在Xcode 10.2.1中可以正常编译。查看didSelect方法。它具有带两个相同外壳的开关块。只是很好奇,它不应该减少编译时错误。该程序也成功运行,没有任何运行时错误。当我单击接受按钮时,第一种情况被执行。

问题:为什么这段代码中没有编译时/运行时错误?

输出:

result

2 个答案:

答案 0 :(得分:2)

sender的类型为UIButton,编译器无法像枚举类型一样进行检查。

由于switch的工作方式与if / else if语句完全相同,因此它仅执行第一个语句。

等效于:

if sender == accept {
    print("Accept button clicked")
}
else if sender == deny {
    print("Deny button clicked")
}
else if sender == accept {
    print("Accept button case again")
}
else {
    break
}

答案 1 :(得分:0)

克服此问题的更好方法,您可以使用TNS_ADMIN属性,在这种情况下,您将能够正确切换它们。

就像this one一样。