6.Runtime官方文档学习--类型编码

官方文档

To assist the runtime system, the compiler encodes the return and argument types for each method in a character string and associates the string with the method selector. The coding scheme it uses is also useful in other contexts and so is made publicly available with the @encode() compiler directive. When given a type specification, @encode() returns a string encoding that type. The type can be a basic type such as an int, a pointer, a tagged structure or union, or a class name—any type, in fact, that can be used as an argument to the C sizeof() operator.

为了帮助运行时系统,编译器把每个方法的返回值和参数类型编码成一个字符串,并把这个字符串和方法选择器关联起来。它使用的编码方案在其他上下文中也有用,因此可通过编译器指令@encode()来公开使用它。当给定一个类型格式,@encode()返回一个编码这个类型的字符串。这个类型可以是一个基础类型,例如int,指针,标记过的结构体或集合,或类名--事实上,任何可以用作C语言中sizeof()操作符的参数的类型都可以。

char *buf1 = @encode(int **);
char *buf2 = @encode(struct key);
char *buf3 = @encode(Rectangle);

The table below lists the type codes. Note that many of them overlap with the codes you use when encoding an object for purposes of archiving or distribution. However, there are codes listed here that you can’t use when writing a coder, and there are codes that you may want to use when writing a coder that aren’t generated by @encode(). (See the NSCoder class specification in the Foundation Framework reference for more information on encoding objects for archiving or distribution.)

下面的表列举了类型编码。注意它们中的许多在为了归档或分发而编码对象时会和你使用的代码发生重叠。然而,列出的编码有的在编写编码器时你无法使用,当你编写编码器时可能想要使用的有些编码不是由@encode()生成的。(有关编码对象的归档或分发的详细信息,请参阅Foundation Framework参考中的NSCoder类规范。)

Table 1 Objective-C type encodings

Code Meaning
c A char
i An int
s A short
l A long. l 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)

Important: Objective-C does not support the long double type. @encode(long double) returns d, which is the same encoding as for double.

重要Objective-C不支持long double类型。@encode(long double)会返回d,和double一样的编码。

The type code for an array is enclosed within square brackets; the number of elements in the array is specified immediately after the open bracket, before the array type. For example, an array of 12 pointers to floats would be encoded as:

数组的类型编码用方括号括起来;数组元素的数量是在开括号之后,数组类型之前立即指定的。例如,一个有12个指向float的指针数组会被编码成:

[12^f]

Structures are specified within braces, and unions within parentheses. The structure tag is listed first, followed by an equal sign and the codes for the fields of the structure listed in sequence. For example, the structure

结构体是在大括号内指定,集合是在小括号内。结构体标记首先会被列出来,接下来是等号和序列中列出来的结构体字段的编码。例如,结构体:

typedef struct example {
    id   anObject;
    char *aString;
    int  anInt;
} Example;

会被编码成:

{example=@*i}

The same encoding results whether the defined type name (Example) or the structure tag (example) is passed to @encode(). The encoding for a structure pointer carries the same amount of information about the structure’s fields:

无论是定义的类型名称(Example)还是结构体标记(example)传递给@encode(),都会产生相同的编码结果。 结构体指针的编码带有关于结构体字段的相同数量的信息:

^{example=@*i}

However, another level of indirection removes the internal type specification:

但是,另一种间接的级别移除了内部类型规范:

^^{example}

Objects are treated like structures. For example, passing the NSObject class name to @encode() yields this encoding:

对象像结构体一样。例如,把NSObject类名传递给@encode()产生这个编码:

{NSObject=#}

The NSObject class declares just one instance variable, isa, of type Class.

NSObject类只声明了一个类型为Class的实例变量isa

Note that although the @encode() directive doesn’t return them, the runtime system uses the additional encodings listed in Table 6-2 for type qualifiers when they’re used to declare methods in a protocol.

注意尽管@encode()指令没有返回它们,但是运行时系统在它们被用于在协议中声明方法时使用如下表中列举的额外的编码来表示类型限定符。

Table 2 Objective-C method encodings

Code Meaning
r const
n in
N inout
o out
O bycopy
R byref
V oneway

你可能感兴趣的:(6.Runtime官方文档学习--类型编码)