Better to use CGRectGetHeight(view.bounds) or view.bounds.size.height directly in Objective-C
Asked Answered
I

1

16

I can't find where I read it, but I remember coming across something that suggested it is better to access height of CGRects using CGRectGetHeight(rect) instead of accessing the variable via rect.size.height

CGFloat height = CGRectGetHeight(self.frame);
// vs  
CGFloat height = self.frame.size.height;

Most of the time, this has to do with views in my use, and I was wondering if there is a real difference (apart from syntax) that separates these two lines of code.

If one is preferential over the other, an explanation of why would be great!

Ineffable answered 7/5, 2014 at 21:16 Comment(0)
F
20

CGRect structures might store height and width in negative values and CGRectGetHeight will always return the positive one. In Swift 3.0 CGRect.height property should be used instead of CGRectGetHeight.

CGRect.height

Regardless of whether the height is stored in the CGRect data structure as a positive or negative number, this function returns the height as if the rectangle were standardized. That is, the result is never a negative number.

Flibbertigibbet answered 7/5, 2014 at 21:20 Comment(4)
That was it, I couldn't remember why it said! Thanks! I'll accept in 10 mins. (when it lets me)Ineffable
More generically, from the CGGeometry reference: “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.”Coreen
In Xcode 8.2.1 using CGRectGetHeight results in a compliation error: “CGRectGetHeight' has been replaced by property ‘CGRect.height’”.Whiteeye
CGRectGetHeight in Swift is CGRect.height. Eg. view.frame.heightTussock

© 2022 - 2024 — McMap. All rights reserved.