Joseph Joseph - 1 month ago 13
iOS Question

Xcode 8 swift 3 building error

I'm having a bit trouble in my code and its giving me a slight error is there a way to fix this problem?

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)
}
}


but thing is I'm only getting an error in the extension UIColor {
and the view.backgroundColor = UIColor < is there any fix?

https://i.stack.imgur.com/f2xNI.png

Answer

Update

Like I mentioned in the comment, keep the extension of UIColor outside of LoginController scope. The correct code looks like below:

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)
    }
}

Isn't that obvious?

You are missing a trailing curly bracket for closing the setupInputsContainerView scope

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)
    }
}