Consider a custom CGRect with a negative width and height:
var rect = CGRect(x: 0.0, y: 0.0, width: -10.0, height: -10.0)
This is a valid rectangle according to Apple's docs because "the rectangle with source [0.0, 0.0] and size [10.0, 10.0] is exactly equivalent to the rectangle with source [10.0, 10.0] and size [-10.0, -10.0] ."
You can standardize this CGRect by calling the obsolete built-in CGRectStandardize method, for example, in Objective-C, or any of the new methods provided in the Swift CGRect :
CGRectStandardize(rect)
But wait! . This will change your rectangle on the coordinate plane, not only making your width and height positive, but making your origin negative to reflect the initial position of the rectangle with its negative width and height.
The built-in CGRectGet functions provide an interface for normalizing a specific value of your rectangle without changing its beginning. Swift provides an extension to CGRect so you can access normalized values directly, instead of using the deprecated C methods provided by CGGeometry :
var rect = CGRect(x: 0.0, y: 0.0, width: -10.0, height: -10.0) rect.size.width // non-normalized, returns -10 CGRectGetWidth(rect) // bridged C function, normalized, returns 10 rect.width // new from Swift extension on CGRect, normalized, returns 10
New interfaces:
extension CGRect {
So, the answer is yes, the same rules for CGRect in Objective-C also apply in Swift. The only difference here is that Swift provides an extension for some CGGeometry structures that allow you to move away from the old C built-in functions connected to CGGeometry headers.