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
编码 | 意义 |
---|---|
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) |
二. Objective-C Method Encodings
编译器内部有些关键字无法用 @encode() 返回,这些关键字也有自己的编码
编码 | 意义 |
---|---|
r | const |
n | in |
N | inout |
o | out |
O | bycopy |
R | byref |
V | oneway |
三. 打印常用类型编码
- (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区别