ios 在 Swift 3 中自动将 CGRectMake 更新为 CGRect

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38335046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:40:44  来源:igfitidea点击:

Update CGRectMake to CGRect in Swift 3 Automatically

iosswiftswift3cgrectmake

提问by yonasstephen

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.

现在 CGRectMake 、 CGPointMake 、 CGSizeMake 等已在 Swift 3.0 中删除,有什么方法可以自动更新所有初始化,例如 from CGRectMake(0,0,w,h) to CGRect(x:0,y:0,width:w,height:h). 手动过程是.. 相当痛苦。

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

不知道为什么 Apple 在我将代码转换为 Current Swift Syntax 时不自动转换它...

采纳答案by Wyetro

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

苹果实际上确实提供了这个功能。你所要做的就是去:

Edit> Convert> To Latest Swift Syntax...

编辑>转换>到最新的 Swift 语法...

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.

这将解决您的语法问题,并且您不必为所有已删除的函数创建新函数。

回答by rob mayoff

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

最简单的解决方案可能只是重新定义 Apple 带走的功能。例子:

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 CGRectMakewill work again.

把它放在你的模块中,所有调用CGRectMake都会再次工作。

回答by matt

Short answer: don't do it. Don't let Apple boss you around. I hateCGRect(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 projectthe instant I start.

简短的回答:不要这样做。不要让 Apple 管你。我讨厌CGRect(x:y:width:height:)。我已经提交了一个错误。我认为初始化程序应该是CGRect(_:_:_:_:),就像CGRectMake. 我已经在 CGRect 上定义了一个扩展来提供它,并在我开始的瞬间将该扩展放入每个单独的项目中。

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.

这样,我所要做的就是在任何地方将“CGRectMake”更改为“CGRect”,我就完成了。