Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGRectGetWidth vs CGRect.size.width

Which is better to use? I prefer CGRect.size.width cause it looks nicer. But, my colleague says CGRectGetWidth is better.

like image 236
ma11hew28 Avatar asked May 11 '11 21:05

ma11hew28


2 Answers

CGRectGetWidth/Height will normalize the width or height before returning them. Normalization is basically just checking if the width or height is negative, and negating it to make it positive if so.

Answered here

like image 150
Nektarios Avatar answered Oct 03 '22 09:10

Nektarios


A rect's width and height can be negative. I have no idea when this would be true in practice, but according to Apple docs:

CGGeometry Reference defines structures for geometric primitives and functions that operate on them. The data structure CGPoint represents a point in a two-dimensional coordinate system. The data structure CGRect represents the location and dimensions of a rectangle. The data structure CGSize represents the dimensions of width and height.

The height and width stored in a CGRect data structure can be negative. For example, a rectangle with an origin of [0.0, 0.0] and a size of [10.0,10.0] is exactly equivalent to a rectangle with an origin of [10.0, 10.0] and a size of [-10.0,-10.0]. Your application can standardize a rectangle—that is, ensure that the height and width are stored as positive values—by calling the CGRectStandardize function. All functions described in this reference that take CGRect data structures as inputs implicitly standardize those rectangles before calculating their results. For this reason, your applications should avoid directly reading and writing the data stored in the CGRect data structure. Instead, use the functions described here to manipulate rectangles and to retrieve their characteristics.

like image 29
memmons Avatar answered Oct 03 '22 08:10

memmons