Is access to CGRect values ​​directly and normalizing them in Swift rules - is Objective-C still valid?

This question is inspired by Andrew Carter 's comment on a previous question about the new CGSize initializer in Swift.

Apple Docs for CGGeometry say:

... your applications should avoid reading and writing directly to data stored in the CGRect Data Structure. Instead, use the functions described here to manipulate the rectangles and retrieve their characteristics.

Is Apple's recommendation not to access data directly in CGRect still valid with Swift? Why use CGRectGetMidX , CGRectGetWidth , etc. Instead of directly accessing the values ​​of the CGRect structure, when these properties are now displayed with the new Swift extension on CGRect ?

+4
source share
2 answers

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) // {x -10 y -10 w 10 h 10} rect.standardized // {x -10 y -10 w 10 h 10} rect.standardizeInPlace() // {x -10 y -10 w 10 h 10} 

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 { // ... public var width: CGFloat { get } public var height: CGFloat { get } public var minX: CGFloat { get } public var midX: CGFloat { get } public var maxX: CGFloat { get } public var minY: CGFloat { get } public var midY: CGFloat { get } public var maxY: CGFloat { get } // ... } 

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.

+8
source

In Swift 3, it looks like CGRectGetWidth() is replaced by CGRect.width .

enter image description here

0
source

Source: https://habr.com/ru/post/1241374/


All Articles