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

StackOverflow https://stackoverflow.com/questions/23528600

문제

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!

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top