Update CGRectMake to CGRect in Swift 3 Automatically

IosSwiftSwift3Cgrectmake

Ios Problem Overview


Now that CGRectMake , CGPointMake, CGSizeMake, etc. has been removed in Swift 3.0, is there any way to automatically update all initializations like from CGRectMake(0,0,w,h) to CGRect(x:0,y:0,width:w,height:h). Manual process is.. quite a pain.

Not sure why Apple don't auto convert this when I convert the code to Current Swift Syntax...

Ios Solutions


Solution 1 - Ios

The simplest solution is probably just to redefine the functions Apple took away. Example:

func CGRectMake(_ x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) -> CGRect {
    return CGRect(x: x, y: y, width: width, height: height)
}

Put that in your module and all calls to CGRectMake will work again.

Solution 2 - Ios

Short answer: don't do it. Don't let Apple boss you around. I hate CGRect(x:y:width:height:). I've filed a bug on it. I think the initializer should be CGRect(_:_:_:_:), just like CGRectMake. I've defined an extension on CGRect that supplies it, and plop that extension into every single project the instant I start.

extension CGRect {
    init(_ x:CGFloat, _ y:CGFloat, _ w:CGFloat, _ h:CGFloat) {
        self.init(x:x, y:y, width:w, height:h)
    }
}

That way, all I have to do is change "CGRectMake" to "CGRect" everywhere, and I'm done.

Solution 3 - Ios

Apple actually does provide this feature. All you have to do is go to:

Edit > Convert > To Latest Swift Syntax...

enter image description here

And then follow the onscreen prompts.

This will solve your syntax issues and you won't have to make new functions for all of the various removed functions.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyonasstephenView Question on Stackoverflow
Solution 1 - Iosrob mayoffView Answer on Stackoverflow
Solution 2 - IosmattView Answer on Stackoverflow
Solution 3 - IosWyetroView Answer on Stackoverflow