Logan Logan -4 years ago 142
Objective-C Question

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

I can't find where I read it, but I remember coming across something that suggested it is better to access height of

CGRect
s 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!

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download