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

etayluz picture etayluz · Sep 23, 2016 · Viewed 8k times · Source

I'm getting a SwiftLint warning on this line:

return UIEdgeInsetsMake(topInset, leftInset, bottomInset, rightInset)

This is the warning :

Legacy Constructor Violation: Swift constructors are preferred over legacy convenience functions. (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)

What is the Swift version for UIEdgeInsetsMake and CGRectInset ?

Answer

keithbhunter picture keithbhunter · Sep 23, 2016

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:

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

The CGRectInset C method was changed to be a method on the CGRect struct.

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