Search code examples
iosswiftcgrectxcode8.2swift3

Updating to latest Swift version problems


I just updated to the latest version of Swift and I am getting issues with a breakpoint error. There are no errors in the console.

backgroundsetting.innerColor = UIColor.rgb(fromHex: 0xB0E7D7)

backgroundsetting.outterColor = UIColor.rgb(fromHex: 0x005E7D)

backgroundsetting.frame = (frame: CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:self.view.frame.size.width, height: self.view.frame.size.height)) 
self.view.addSubview(backgroundsetting)

Then after the update it wanted me to set the backgroundsetting.frame as! CGRect and it crashes the app when I open that part of the app. Why is this occurring? Here is the after code:

backgroundsetting.innerColor = UIColor.rgb(fromHex: 0xB0E7D7)

backgroundsetting.outterColor = UIColor.rgb(fromHex: 0x005E7D)

backgroundsetting.frame = (frame: CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:self.view.frame.size.width, height: self.view.frame.size.height)) as! CGRect
self.view.addSubview(backgroundsetting)

Solution

  • The backgroundsetting.frame property is a type of CGRect, why not creating a CGRect like this:

    backgroundsetting.frame = CGRect(x: self.view.frame.size.width * 0, y: self.view.frame.size.height * 0, width:self.view.frame.size.width, height: self.view.frame.size.height)