objective-c中关于类型编码的解释

简介:

在某些情况下,我们需要动态的向一个类插入一个实例方法(也可以是一个类方法);这时我们可以用class_addMethod函数来完成:

 BOOL class_addMethod ( Class cls, SEL name, IMP imp, const char *types ); 

在Objective-C Runtime Reference 中可以看到各个参数的含义:

Parameters
**cls**     
The class to which to add a method.

**name**    
A selector that specifies the name of the method being added.

**imp**     
A function which is the implementation of the new method. The function must take at least two arguments—self and _cmd.

**types**   
An array of characters that describe the types of the arguments to the method. For possible values, see Objective-C Runtime Programming Guide > Type Encodings. Since the function must take at least two arguments—self and _cmd, the second and third characters must be “@:” (the first character is the return type).

前面几个参数比较好理解,我们来看看最后一个:这是一个字符串按照imp的签名依次排列返回值和各个参数。apple开发者官网有对应的编码:
Type Encodings

我们也可以用@encode来查看实际的编码,比如:

NSLog(@"%s",@encode(int *));
相关文章
|
iOS开发
Objective-C特有类型——id
Objective-C特有类型——id OC里,id和int、double等一样,是一个类型 不同的是: id是一个万能指针,能指向/操作任何OC对象 相当于 (NS...
731 0
|
iOS开发 Java JavaScript
Objective-C 对 URL 进展 URLEncode 编码
<span style="padding:0px; margin:0px; color:rgb(102,102,102); font-size:14px; font-family:微软雅黑; line-height:24px">Objective-C 对 URL 进行 URLEncode 编码</span><br style="padding:0px; margin:0px; font-s
1501 0
|
iOS开发 C++
Objective-C学习——中文URL编码和解码
<p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; font-family:Arial; font-size:14px; line-height:26px"> 发现NSString类中有内置的方法可以实现。他们分别是:</p> <p style="margin-top:0px
1407 0
|
iOS开发
【《Objective-C基础教程 》笔记ch02】(二)Boolean类型及实例
一、布尔类型         布尔类型是一种对带符号的字符类型(signed char)的类型定义,使用8位的存储空间。         通过#define指令把YES定义为1,NO定义为0,都是8位的二进制数。
969 0
|
iOS开发
Objective-C - 类型
1. Strings 1) 简单字符串 NSString *aString = @"this is a string"; NSString *anotherString = @"and this is another one"; 2) 对象初始化 NSString *aStri...
585 0
|
iOS开发
Objective-C 类型
1. Strings 1) 简单字符串 NSString *aString = @"this is a string"; NSString *anotherString = @"and this is another one"; 2) 对象初始化 NSString *aStri...
551 0