Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 417
Logan Avatar asked May 07 '14 21:05

Logan


1 Answers

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.

like image 200
Avt Avatar answered Oct 19 '22 18:10

Avt