Objective-C 类型编码

Posted SSIrreplaceable

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Objective-C 类型编码相关的知识,希望对你有一定的参考价值。

在开发的时候我们会遇到后跟 ObjCType:(const char *)types 的方法。

如:
+ (NSValue *)valueWithBytes:(const void *)value objCType:(const char *)type;
+ (nullable NSMethodSignature *)signatureWithObjCTypes:(const char *)types;

ObjCTypes 的参数需要用 Objective-C 的编译器指令 @encode() 来创建,@encode() 返回的是 Objective-C 类型编码(Objective-C Type Encodings)。

一. Objective-C Type Encodings

编码意义
cA char
iAn int
sA short
lA longl is treated as a 32-bit quantity on 64-bit programs.
qA long long
CAn unsigned char
IAn unsigned int
SAn unsigned short
LAn unsigned long
QAn unsigned long long
fA float
dA double
BA C++ bool or a C99 _Bool
vA 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
bnumA bit field of num bits
^typeA pointer to type
?An unknown type (among other things, this code is used for function pointers)

二. Objective-C Method Encodings

编译器内部有些关键字无法用 @encode() 返回,这些关键字也有自己的编码

编码意义
rconst
nin
Ninout
oout
Obycopy
Rbyref
Voneway

三. 打印常用类型编码

- (void)viewDidLoad 
    [super viewDidLoad];

    NSLog(@"基本数据类型:");
    NSLog(@"short               \\t\\t %s", @encode(short));
    NSLog(@"int                 \\t\\t %s", @encode(int));
    NSLog(@"long                \\t\\t %s", @encode(long));
    NSLog(@"ong long            \\t\\t %s", @encode(long long));
    NSLog(@"float               \\t\\t %s", @encode(float));
    NSLog(@"double              \\t\\t %s", @encode(double));
    NSLog(@"char                \\t\\t %s", @encode(char));

    printf("\\n");

    NSLog(@"指针和数组类型:");
    NSLog(@"int *               \\t\\t %s", @encode(int *));
    NSLog(@"int **              \\t\\t %s", @encode(int **));
    NSLog(@"int ***             \\t\\t %s", @encode(int ***));
    NSLog(@"int []              \\t\\t %s", @encode(int []));
    NSLog(@"int [2]             \\t\\t %s", @encode(int [2]));
    NSLog(@"int [][3]           \\t\\t %s", @encode(int [][3]));
    NSLog(@"int [3][3]          \\t\\t %s", @encode(int [3][3]));
    NSLog(@"int [][4][4]        \\t\\t %s", @encode(int [][4][4]));
    NSLog(@"int [4][4][4]       \\t\\t %s", @encode(int [4][4][4]));

    printf("\\n");

    NSLog(@"空类型:");
    NSLog(@"void                \\t\\t %s", @encode(void));
    NSLog(@"void *              \\t\\t %s", @encode(void *));
    NSLog(@"void **             \\t\\t %s", @encode(void **));
    NSLog(@"void ***            \\t\\t %s", @encode(void ***));

    printf("\\n");

    NSLog(@"结构体类型:");
    struct Person 
        char *anme;
        int age;
        char *birthday;
    ;
    NSLog(@"struct Person       \\t\\t %s", @encode(struct Person));
    NSLog(@"CGPoint             \\t\\t %s", @encode(CGPoint));
    NSLog(@"CGRect              \\t\\t %s", @encode(CGRect));


    printf("\\n");

    NSLog(@"OC类型:");
    NSLog(@"BOOL                   \\t\\t %s", @encode(BOOL));
    NSLog(@"SEL                    \\t\\t %s", @encode(SEL));
    NSLog(@"id                     \\t\\t %s", @encode(id));
    NSLog(@"Class                  \\t\\t %s", @encode(Class));
    NSLog(@"Class *                \\t\\t %s", @encode(Class *));
    NSLog(@"NSObject class         \\t\\t %s", @encode(typeof([NSObject class])));
    NSLog(@"[NSObject class] *     \\t\\t %s", @encode(typeof([NSObject class]) *));
    NSLog(@"NSObject               \\t\\t %s", @encode(NSObject));
    NSLog(@"NSObject *             \\t\\t %s", @encode(NSObject *));
    NSLog(@"NSArray                \\t\\t %s", @encode(NSArray));
    NSLog(@"NSArray *              \\t\\t %s", @encode(NSArray *));
    NSLog(@"NSMutableArray         \\t\\t %s", @encode(NSMutableArray));
    NSLog(@"NSMutableArray *       \\t\\t %s", @encode(NSMutableArray *));
    NSLog(@"UIView                 \\t\\t %s", @encode(UIView));
    NSLog(@"UIView *               \\t\\t %s", @encode(UIView *));
    NSLog(@"UIImage                \\t\\t %s", @encode(UIImage));
    NSLog(@"UIImage *              \\t\\t %s", @encode(UIImage *));

打印结果:


总结:

  • * 的编码为 ^

  • OC对象的编码为 @

  • Class(类)编码为 #

  • SEL类型编码为 :

以上是关于Objective-C 类型编码的主要内容,如果未能解决你的问题,请参考以下文章

在 Objective-c 中使用 AFNetworking 对字典进行 URL 编码

Objective-C中的instancetype和id区别

iOS开发系列--Objective-C 之 KVCKVO

objective-C nil,Nil,NULL 和NSNull的小结

Objective-C编码规范[译]

objective-c开发——地图定位之地理编码和地理反编码