Update CGRectMake to CGRect in Swift 3 Automatically
Asked Answered
S

3

54

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...

Sisterinlaw answered 12/7, 2016 at 17:3 Comment(1)
Convert to Current Swift Syntax works for me and converts CGRectMake(0,0,w,h) to CGRect(x: 0,y: 0,width: w,height: h). If it does not work in your case then you should file a bug report.Fick
M
19

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.

Moneyed answered 12/7, 2016 at 19:4 Comment(2)
it does yeah! for some reasons when I first convert, some files didn't get converted. But when I ran again, it did! I guess must run multiple times until all your files get converted..Sisterinlaw
it doesnt do it for me, i've more than once and even after making some changes.Ruin
O
79

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.

Outlier answered 12/7, 2016 at 17:12 Comment(5)
hmm that's cool! let me see what others think; else, I'll accept yours as answers!Sisterinlaw
I've been waiting for PaintCode to update to Swift 3. With all the CGRectMakes in there, this was by far the best solution.Lavoisier
Why is there a CGRectMake in the first place if you can init with CGRect the same way, i don't get itXavier
There used to be CGRectMake because Apple hadn't fine-tuned the way Core Graphics is imported into Swift. They now have, so CGRectMake is not available in current Swift releases.Outlier
Obj-C people suffering the migration to Swift :-) That solution is quick and dirty. I'd to it for a quick port. But if you're using Swift, better to just use the paradigm. Not as bad as it seems at first. If I was really bugged by labels, I'd make an extension. Kind of what you get used to. I've gone back and forth over my career with cryptic/terse vs. long/labeled and they both have advantages. Labels overall make code much easier to maintain and reverse engineer, because after awhile away you do forget and it's a hassle to have to look up every function just to figure out unnamed arguments.Hyperthermia
A
32

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.

Accredit answered 13/7, 2016 at 20:35 Comment(3)
Thanks for this great helpful answer... You saved my time.Oquassa
where can i add this extension? rather, how do i use it? thanksRuin
@nights, outside any classIdalia
M
19

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.

Moneyed answered 12/7, 2016 at 19:4 Comment(2)
it does yeah! for some reasons when I first convert, some files didn't get converted. But when I ran again, it did! I guess must run multiple times until all your files get converted..Sisterinlaw
it doesnt do it for me, i've more than once and even after making some changes.Ruin

© 2022 - 2024 — McMap. All rights reserved.