Xcode 8 Swift 3构建错误

时间:2016-10-28 13:26:04

标签: ios xcode

我的代码给了我一个错误。我该如何解决?

   override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor (r: 13, g: 214, b: 151)

        view.addSubview(inputsContainerView)


    setupInputsContainerView()

    }

    func setupInputsContainerView() {

        //need x, y, width, height constraints
        inputsContainerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        inputsContainerView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        inputsContainerView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -24).isActive = true
        inputsContainerView.heightAnchor.constraint(equalToConstant: 150).isActive = true

    func  PreferredStatusBarStyle() ->UIStatusBarStyle {
    return.lightContent
    }
}

extension UIColor {

    convenience init(r: CGFloat, g: CGFloat, b: CGFloat) {
        self.init(red: r/255, green: g/255 , blue: b/255, alpha: 1)
    }
}

但事情是我只是在扩展名UIColor中出错了{ 和view.backgroundColor = UIColor<有没有修复?

2 个答案:

答案 0 :(得分:0)

更新

就像我在评论中提到的那样,将UIColor的扩展名保留在LoginController范围之外。正确的代码如下所示:

class LoginController: UIViewController {

    let inputsContainerView: UIView =  {
        let view = UIView()
        let inputsContainerView = UIView()
        inputsContainerView.backgroundColor = UIColor.white
        inputsContainerView.translatesAutoresizingMaskIntoConstraints = false
        view.layer.cornerRadius = 5
        view.layer .masksToBounds = true
        return view

    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor (r: 13, g: 214, b: 151)
        view.addSubview(inputsContainerView)

        setupInputsContainerView()
    }

    func setupInputsContainerView() {
        //need x, y, width, height constraints
        inputsContainerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        inputsContainerView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        inputsContainerView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -24).isActive = true
        inputsContainerView.heightAnchor.constraint(equalToConstant: 150).isActive = true
    }

    func PreferredStatusBarStyle() -> UIStatusBarStyle {
        return.lightContent
    }
}

extension UIColor {
    convenience init(r: Int, g: Int, b: Int) {
        self.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0 , blue: CGFloat(b)/255.0, alpha: 1)
    }
}

不是很明显吗?

您缺少用于关闭setupInputsContainerView范围

的尾随花括号
override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = UIColor (r: 13, g: 214, b: 151)

    view.addSubview(inputsContainerView)


    setupInputsContainerView()

}

func setupInputsContainerView() {

    //need x, y, width, height constraints
    inputsContainerView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    inputsContainerView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    inputsContainerView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -24).isActive = true
    inputsContainerView.heightAnchor.constraint(equalToConstant: 150).isActive = true
}

func  PreferredStatusBarStyle() ->UIStatusBarStyle {
    return.lightContent
}

extension UIColor {

    convenience init(r: CGFloat, g: CGFloat, b: CGFloat) {
        self.init(red: r/255, green: g/255 , blue: b/255, alpha: 1)
    }
}

答案 1 :(得分:0)

您传递Int类型的参数而不是CGFloat。我建议更改自定义init方法单一(使用Int params而不是CGFloat)并将Int参数转换为CGFloat,并传递给框架初始化方法。

extension UIColor {

    convenience init(r: Int, g: Int, b: Int) {
        self.init(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0 , blue: CGFloat(b)/255.0, alpha: 1)
    }
}