What does inout CGPoint * mean as a parameter?

The UIScrollView delegate methods have:

scrollViewWillEndDragging:withVelocity:targetContentOffset:

and the last parameter:

(inout CGPoint *)targetContentOffset

I am curious what inout means and why CGPoint is a pointer. I tried to print targetContentOffset on the console, but I'm not sure how to do this. Any help is appreciated!

+7
ios objective-c uiscrollview uiscrollviewdelegate
source share
1 answer

This means that the parameter is used to send in data, as well as to receive out data.

Let's see an example implementation:

 - (void)increment:(inout NSUInteger*)number { *number = *number + 1; } NSUInteger someNumber = 10; [self increment:&someNumber]; NSLog(@"Number: %u", someNumber); //prints 11 

This pattern is often used with C structures, such as CGPoint , because they are passed by value (copied).

Also note that inout is absolutely not required here, but it helps the compiler to understand the meaning of your code and improve performance during optimization.

There are also separate in and out annotations, but as far as I know, there are no compiler warnings when you use them incorrectly, and it seems that Apple no longer uses them.

+22
source share

All Articles