Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does inout CGPoint * mean as a parameter?

In the UIScrollView delegate methods, there's:

scrollViewWillEndDragging:withVelocity:targetContentOffset:

and the last parameter is:

(inout CGPoint *)targetContentOffset

I'm curious as to what the inout means and why CGPoint is a pointer. I tried printing targetContentOffset to the console, but I'm not sure how to do so as well. Any help is appreciated!

like image 878
c0sic Avatar asked Sep 17 '13 17:09

c0sic


1 Answers

It means the parameter is used to send data in but also to get data out.

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, e.g CGPoint because they are passed by value (copied).

Also note that inout is not absolutely required there but it helps the compiler to understand the meaning of your code and do better job when optimizing.

There are also separate in and out annonations but as far as I know there are no compiler warnings when you misuse them and it doesn't seem Apple is using them any more.

like image 117
Sulthan Avatar answered Oct 01 '22 10:10

Sulthan