Objective-C in, out, inout, byref, byval, .. and so on. Who are they?

I discovered something unfamiliar while reading the Objective-C @encoding for @encoding .

 Table 6-2 Objective-C method encodings Code Meaning r const n in N inout o out O bycopy R byref V oneway 

The only thing I know is oneway . What others?

+61
objective-c
Apr 10 '11 at 3:23
source share
4 answers

These are annotations of the method parameters and return values ​​used by Distributed Objects. I say because, apparently, they no longer sign them in the documentation for Apples. Previously, there was the Deleted Messages section in the Objective-C Programming Language document, which is still referenced by the Common Objects document.

  • in: argument is only an input argument and will not be mentioned later
  • out: argument is only an output argument used to return a value by reference
  • inout: - input and output argument
  • const: argument (pointer) constant
  • bycopy: instead of proxy / NSDistantObject , pass or return a copy of the object
  • byref: use proxy object (default)
+80
Apr 10 2018-11-11T00:
source share

Besides distributed objects, one of these annotations appears to be used by ARC. I came across the following description of cla going to the output parameter by writing back :

If the parameter is not an Objective-C parameter parameter labeled out , then *p is read, and the result is written to temporary primitive semantics.

This is due to methods such as - (BOOL)executeWithError:(out NSError **)error .

Ignoring the out keyword, ARC has the correct behavior for processing a reference object passing as __autoreleasing , so ARC treats the error parameter as of type NSError * __autoreleasing * . If you use a variable other than the other, ARC will add a temporary pass to the autoreleasing variable in the function (for consistency):

Source

 NSError *error; [obj executeWithError:&error]; 

Pseudotransformed code

 NSError * __strong error; NSError * __autoreleasing temp; temp = error; [obj executeWithError:&temp]; error = temp; 

With the above code, the string temp = error would not be needed if we somehow knew that temp would never be counted. This is where the out annotation takes effect. In the cited description, if out absent, the compiler should add the line temp = error , but if it contains out , it can exclude the line and make the code a little smaller / faster. With out converted code becomes:

 NSError * __strong error; NSError * __autoreleasing temp; [obj executeWithError:&temp]; error = temp; 

Of course, if you are worried about binary size and speed, you should simply encode the following:

 NSError * __autoreleasing error; [obj executeWithError:&error]; 

It is possible that these annotations are used elsewhere throughout the compiler and at run time, and may be used in other places in the future. Personally, I like to use out as a hint to other developers that I will not read the meaning.

+21
Sep 23 '13 at 22:49
source share

If anyone has stumbled upon this post and has the same confusion as me, the argument "in" could also be a keyword, which is a quick listing. See here for more details.

0
Jan 25 '12 at 13:30
source share

You can read Objective-C sources at http://www.opensource.apple.com/source/objc4/objc4-437.1/ to understand what these annotations mean.

-3
Apr 10 2018-11-11T00:
source share



All Articles