CGRectZero vs CGRect.zeroRect?

ma11hew28 picture ma11hew28 · May 2, 2015 · Viewed 17.2k times · Source

Both seem to work the same in Swift. Which should we use?

  1. CGRectZero vs CGRect.zeroRect
  2. GRectMake(1, 1, 1, 1) vs CGRect(x: 1, y: 1, width: 1, height: 1) UIEdgeInsetsMake(1, 1, 1, 1) vs UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
  3. Other patterns

Answer

rob mayoff picture rob mayoff · May 2, 2015

UPDATE2

In Swift 3, CGRectZero is no longer available. CGRect.zero is your only choice. Learn it, know it, live it.

UPDATE

CGRect.zeroRect was renamed to CGRect.zero in iOS 9.0. That is close enough to CGRectZero (and the same number of keystrokes, counting shift as a keystroke) that I'd recommend CGRect.zero to anyone who's not an Objective-C coder in the habit of using CGRectZero.

ORIGINAL

There's no particular reason to favor one or the other. You should use whichever style you or your team prefers.

Personally, I'd use CGRectZero because it's shorter and I'm used to it from Objective-C.

I'd go with CGRectMake for the same reason.

I'd probably use UIEdgeInsets(top:left:bottom:right:) because I rarely see instances of UIEdgeInsets, so I'm less likely to remember the order of the arguments in UIEdgeInsetsMake. (You get placeholders for it when you're typing in a call to UIEdgeInsetsMake, but when you're reading existing code there's no hints about the argument order.)