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类型编码为 :

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值