Code | Meaning |
---|---|
c | A char |
i | An int |
s | A short |
l | A longl is treated as a 32-bit quantity on 64-bit programs. |
q | A long long |
C | An unsigned char |
i | An unsigned int |
S | An unsigned short |
L | An unsigned long |
Q | An unsigned long long |
f | A float |
d | A double |
B | A C++ bool or a C99 _Bool |
v | A void |
* | A character string (char *) |
@ | An object (whether statically typed or typed id) |
# | A class object (Class) |
: | A method selector (SEL) |
[array type] | An array |
{name=type...} | A structure |
(name=type...) | A union |
bnum | A bit field of num bits |
^type | A pointer to type |
? | An unknown type (among other things, this code is used for function pointers) |
NSLog(@"int : %s", @encode(int));
NSLog(@"float: %s", @encode(float));
NSLog(@"float *: %s", @encode(float*));
NSLog(@"char: %s", @encode(char));
NSLog(@"char *: %s", @encode(char *));
NSLog(@"BOOL: %s", @encode(BOOL));
NSLog(@"void: %s", @encode(void));
NSLog(@"void *: %s", @encode(void *));
NSLog(@"NSObject * : %s", @encode(NSObject *));
NSLog(@"NSObject : %s", @encode(NSObject));
NSLog(@"[NSObject] : %s", @encode(typeof([NSObject class])));
NSLog(@"NSError ** : %s", @encode(typeof(NSError **)));
int intArray[5] = {1, 2, 3, 4, 5};
NSLog(@"int[] : %s", @encode(typeof(intArray))); !
float floatArray[3] = {0.1f, 0.2f, 0.3f};
NSLog(@"float[] : %s", @encode(typeof(floatArray))); !
typedef struct _struct
{
short a;
long long b;
unsigned long long c;
} Struct;
NSLog(@"struct : %s", @encode(typeof(Struct)));
int :i
float :f
float * :^f
char :c
char * :*
BOOL :c
void :v
void * :^v
NSObject * : @
NSObject : #
[NSObject] : {NSObject=#}
NSError ** : ^@
int[]: [5i]
float[]: [3f]
struct: {_struct=sqQ}
There are some interesting takeaways from this:
Whereas the standard encoding for pointers is a preceding ^, char gets its own code: . This makes sense conceptually, since C strings are thought to be entities in and of themselves, rather than a pointer to something else.
BOOL is c, rather than i, as one might expect. Reason being, char is smaller than an int, and when Objective-C was originally designed in the 80's, bits (much like the US Dollar) were more valuable than they are today.
Passing NSObject directly yields #. However, passing [NSObject class] yields a struct named NSObject with a single class field. That is, of course, the isa field, which all NSObject instances have to signify their type.
As mentioned in Apple's "Objective-C Runtime Programming Guide", there are a handful of type encodings that are used internally, but cannot be returned with @encode.
These are the type qualifiers for methods declared in a protocol:
Code | Meaning |
---|---|
r | const |
n | in |
N | inout |
o | out |
O | bycopy |
R | byref |
V | oneway |