[#0x0032] Pointer Swizzling

  继续学习。pointer swizzling有个很坏的翻译叫“指针混写”,翻译得不知所云。

  以下来自Jargon File和Wikipedia。

 

  To convert external names, array indices, or references within a data structure into address pointers when the data structure is brought into main memory from external storage; this may be done for speed in chasing references or to simplify code (e.g., by turning lots of name lookups into pointer dereferences). The converse operation is sometimes termed ‘unswizzling'.

 

  Jargon File的这一段解释得还是不怎么清楚,看Wikipedia上的这个例子就很清楚了。

 

  In computer science, pointer swizzling is the conversion of references based on name or position to direct pointer references. It is typically performed during the deserialization (loading) of a relocatable object from disk, such as an executable file or pointer-based data structure. The reverse operation, replacing pointers with position-independent symbols or positions, is sometimes referred to as unswizzling, and is performed during serialization(saving).

 

  For example, suppose we have the following linked list data structure:

struct node {
        int data;
        struct node *next;
};

  We can easily create a linked list data structure in memory using such an object, but when we attempt to save it to disk we run into trouble. Directly saving the pointer values won't work on most architectures, because the next time we load it the memory positions the nodes now use may be in use by other data. One way of dealing with this is to assign a unique id number to each node and then unswizzle the pointers by turning them into a field indicating the id number of the next node:

struct node_saved {
        int data;
        int id_number;
        int id_number_of_next_node;
};

  We can save these records to disk in any order, and no information will be lost. Other options include saving the file offset of the next node or a number indicating its position in the sequence of saved records.

 

  When we go to load these nodes, however, we quickly discover that attempting to find a node based on its number is cumbersome and inefficient. We'd like our original data structure back so we can simply follow next pointers to traverse the list. To do this, we perform pointer swizzling, finding the address of each node and turning the id_number_of_next_node fields back into direct pointers to the right node.

 

  简单地说来,就是内存中的节点间通过“逻辑”指针(实质是内存地址)连接,而将这些节点保存到磁盘时,“逻辑”指针就没有任何意义了,需要变换一种方式来表示这些节点间的连接关系(这里也不好叫做“物理”指针……),这个变换的过程称为unswizzling。反过来,将这些节点从磁盘load到内存中时的变换就是swizzling。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TableView 无数据 runtime method swizzling 是一种常用的技术手段,用于在 TableView 中没有数据时,自动地替换原有的方法实现来展示自定义的占位图或提示信息。 在 iOS 开发中,当 TableView 没有数据时,通常会显示一张空白的背景或者一些提示文字,告诉用户当前没有任何数据。而使用 runtime method swizzling 技术,我们可以在 TableView 的相关方法中插入自定义的代码,从而实现自动切换显示空白背景或者提示信息。 具体的实现步骤如下: 1. 创建一个自定义的占位图或提示信息视图,以便在没有数据时显示在 TableView 上。 2. 通过 runtime method swizzling 技术,将 TableView 的 reloadData 方法替换为我们自定义的方法实现。 3. 在自定义的方法实现中,判断 TableView 数据源的数量,如果为零,则将自定义的占位图或提示信息视图添加到 TableView 上,并将 TableView 的背景设置为透明。 4. 如果数据源数量不为零,则将 TableView 的背景设置为默认的 TableView 背景,并调用原有的 reloadData 方法来刷新 TableView。 使用 runtime method swizzling 技术来实现 TableView 无数据时的自定义占位图或提示信息的展示可以提高开发效率,减少了代码的重复编写。同时,由于是替换方法的实现,所以不会对原有的代码产生太多影响,维护成本也较小。但是需要注意的是,使用 runtime method swizzling 技术需要谨慎,遵循苹果官方的 API 规范,以免引发一些潜在的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值