xcode Legacy Constructor Violation:Swift 构造函数比遗留的便利函数更受欢迎。(legacy_constructor)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39663734/
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-09-15 09:20:16  来源:igfitidea点击:

Legacy Constructor Violation: Swift constructors are preferred over legacy convenience functions. (legacy_constructor)

swiftxcodeswiftlint

提问by etayluz

I'm getting a SwiftLintwarning on this line:

我在这一行收到SwiftLint警告:

return UIEdgeInsetsMake(topInset, leftInset, bottomInset, rightInset)

This is the warning :

这是警告:

Legacy Constructor Violation: Swift constructors are preferred over legacy convenience functions. (legacy_constructor)

Legacy Constructor Violation:Swift 构造函数比遗留的便利函数更受欢迎。(legacy_constructor)

I'm getting a warning on this line as well:

我也在这条线上收到警告:

return CGRectInset(bounds, insetX, insetY)

Legacy CGGeometry Functions Violation: Struct extension properties and methods are preferred over legacy functions (legacy_cggeometry_functions)

遗留 CGGeometry 函数违规:结构扩展属性和方法优于遗留函数(legacy_cggeometry_functions)

What is the Swift version for UIEdgeInsetsMakeand CGRectInset?

UIEdgeInsetsMake和的 Swift 版本是CGRectInset什么?

回答by keithbhunter

Swift wants you to update to the new struct initializers for those types, instead of the old C constructors. So your inset initializer would be changed to this:

Swift 希望你更新这些类型的新结构初始化器,而不是旧的 C 构造器。因此,您的插入初始化程序将更改为:

return UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)

The CGRectInsetC method was changed to be a method on the CGRectstruct.

CGRectInsetC法改变为是对一个方法CGRect结构。

return bounds.insetBy(dx: insetX, dy: insetY)