16

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 ?

SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85
etayluz
  • 15,920
  • 23
  • 106
  • 151
  • Did you have a look at the reference https://developer.apple.com/reference/uikit/uiedgeinsets? Or try auto-completion: `return UIEdgeInsets(` – Martin R Sep 23 '16 at 14:53
  • I did, but the Swift version bears the same name. So I don't know how to resolve this warning or if there is a different way of doing it. – etayluz Sep 23 '16 at 14:54
  • 6
    The type is `UIEdgeInsets`, so start with `UIEdgeInsets(`, *not* `UIEdgeInsetsMake`... – Martin R Sep 23 '16 at 14:55
  • Thank you! What about CGRectInset ? – etayluz Sep 23 '16 at 14:58
  • 1
    Again, lookup up the CGRectInset documentation page, then switch to Swift, and you'll find https://developer.apple.com/reference/coregraphics/cgrect/1454218-insetby: `func insetBy(dx: CGFloat, dy: CGFloat) -> CGRect` – Martin R Sep 23 '16 at 15:03

1 Answers1

17

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)
keithbhunter
  • 12,258
  • 4
  • 33
  • 58