Type Encodings

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html#//apple_ref/doc/uid/TP40008048-CH100-SW1

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.

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.)

Table 6-1  Objective-C type encodings

Code


Meaning


c


char


i


An int


s


short


l


long

l is treated as a 32-bit quantity on 64-bit programs.


q


long long


C


An unsigned char


I


An unsigned int


S


An unsigned short


L


An unsigned long


Q


An unsigned long long


f


float


d


double


B


A C++ bool or a C99 _Bool


v


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.

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^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;

would be encoded like this:

{[email protected]*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:

^{[email protected]*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=#}

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

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.

时间: 2024-08-28 01:26:08

Type Encodings的相关文章

iOS开发——语法篇OC篇&高级语法精讲二

Objective高级语法精讲二 Objective-C是基于C语言加入了面向对象特性和消息转发机制的动态语言,这意味着它不仅需要一个编译器,还需要Runtime系统来动态创建类和对象,进行消息发送和转发.下面通过分析Apple开源的Runtime代码(我使用的版本是objc4-646.tar)来深入理解Objective-C的Runtime机制. Runtime数据结构 在Objective-C中,使用[receiver message]语法并不会马上执行receiver对象的message方

Objective-C & Runtime

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html About Objective-C Objective-C is the primary programming language you use when writing software for OS X and iOS. It's

深入解析 ObjC 中方法的结构

因为 ObjC 的 runtime 只能在 Mac OS 下才能编译,所以文章中的代码都是在 Mac OS,也就是 x86_64 架构下运行的,对于在 arm64 中运行的代码会特别说明. 在上一篇分析 isa 的文章从 NSObject 的初始化了解 isa中曾经说到过实例方法被调用时,会通过其持有 isa 指针寻找对应的类,然后在其中的 class_data_bits_t 中查找对应的方法,在这一篇文章中会介绍方法在 ObjC 中是如何存储方法的. 这篇文章的首先会根据 ObjC 源代码来分

iOS runtime和runloop

runtime 和 runloop 作为一个程序员进阶是必须的,也是非常重要的, 在面试过程中是经常会被问到的, 所以大家有必要进行研究,有能力的童鞋可以和下面作者一样, 亲历实践一下. 在简书里发现了两篇非常好的文章介绍 runtime和runloop的,在这里合二为一了, 把原版作者的东西拿了过来, 为了尊重作者,在这里注明一下 @sam_lau 是runtime的作者, @tripleCC是runloop的作者   RunTime Objective-C是基于C语言加入了面向对象特性和消息

【原】FMDB源码阅读(二)

[原]FMDB源码阅读(二) 本文转载请注明出处 -- polobymulberry-博客园 1. 前言 上一篇只是简单地过了一下FMDB一个简单例子的基本流程,并没有涉及到FMDB的所有方方面面,比如FMDB的executeUpdate:系列方法.数据库的加解密等等.这次写的就是对FMDatabase和FMResultSet这两个文件的补全内容.每次写这种补全的内容最头疼,内容会很分散,感觉没啥条理. 2. executeUpdate:系列函数 注意除了"SELECT"语句外,其他的

oc消息发送机制之performSelector方法的扩充

本文摘自别的博客 各种语言都有些传递函数的方法:C语言中可以使用函数指针,C++中有函数引用.仿函数和lambda,Objective-C里也有选择器(selector)和block. 不过由于iOS SDK中的大部分API都是selector的方式,所以本文就重点讲述selector了.Objective-C和我接触过的其他面向对象的语言不同,它强调消息传递,而非方法调用.因此你可以对一个对象传递任何消息,而不需要在编译期声名这些消息的处理方法. 很显然,既然编译期并不能确定方法的地址,那么运

iOS Objc Runtime 教程+实例Demo

例子Demo 欢迎给我star!我会继续分享的. 概述 Objc Runtime使得C具有了面向对象能力,在程序运行时创建,检查,修改类.对象和它们的方法.Runtime是C和汇编编写的,这里http://www.opensource.apple.com/source/objc4/可以下到苹果维护的开源代码,GNU也有一个开源的runtime版本,他们都努力的保持一致. 应用场景 将某些OC代码转为运行时代码,探究底层,比如block的实现原理 拦截系统自带的方法调用(Swizzle 黑魔法),

NSObject头文件解析 / 消息机制 / Runtime解读 (一)

NSObject头文件解析 当我们需要自定义类都会创建一个NSObject子类, 比如: #import <Foundation/Foundation.h> @interface ClassA : NSObject @end 那么NSObject里面具体有什么呢? 我们点到它的头文件里面去看看 @interface NSObject <NSObject> { Class isa OBJC_ISA_AVAILABILITY; //每个NSObject对象都拥有一个Class类作为成员

Objective-C:runtime

Objective-C:runtime Runtime系统是一个由一系列C语言函数和数据结构组成的动态共享库,即通过面向过程语言C实现Objective-C语言的面向对象特性. 1 .概述 Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行时来处理.这种特性意味着Objective-C不仅需要一个编译器,还需要一个运行时系统来执行编译的代码.对于Objective-C来说,这个运行时系统就像一个操作系统一样:它让所有的工作可以正常的运行,这个运行时系统即O