Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the @encode compiler directive implemented in Objective-C?

Can anyone explain how @encode works to extract the datatype elements present in a given object, struct, or datatype into a type definition to be used as a class descriptor for instantiation?

Or maybe a pointer to some resources for learning about the implementation of new preprocessor directives?

like image 843
Anderson Avatar asked Feb 11 '10 19:02

Anderson


2 Answers

The @encode directive parses the provided type and generates a constant string based on that type. The encoding of all C primitive types (including signed and unsigned versions) and the Objective-C id and SEL types all have single-character encodings, these can be found in <objc/runtime.h>. More complicated types such as structs and arrays have larger encodings.

More information is available in the Objective-C Runtime Programming Guide [PDF].

like image 107
dreamlax Avatar answered Oct 12 '22 19:10

dreamlax


The phrasing of the original question may have been unclear and I think that my mentioning of a possible implementation involving the preprocessor caused the conversation to turn toward the subtleties of how compilers work rather than the intended question.

Please reference this question, which I believe is much more clear as to what I'm trying to learn: How would I implement something similar to the Objective-C @encode() compiler directive in ANSI C?

like image 29
Anderson Avatar answered Oct 12 '22 20:10

Anderson