swizzle 特定方法_Swift中的Swizzle方法

swizzle 特定方法

The Swizzle Method practice is a tool every iOS Developer should be aware of.

Swizzle方法实践是每个iOS开发人员都应了解的工具。

I recently came across the swizzle method practice and thought I’d look into it.

最近,我遇到了麻烦的方法实践,以为我会研究一下。

I immediately realized that this technique should be integrated into the toolbox of every iOS developer, so I decided to write this article.

我立即意识到,该技术应该集成到每个iOS开发人员的工具箱中,因此我决定写这篇文章。

Method swizzling is the process that changes the implementation of an existing selector.

方法混乱 是更改现有选择器的实现的过程。

This is made possible by the fact that in Objective-C you can change method invocations at runtime thanks to Dynamic dispatch. This is nothing more than the process of selecting which implementation of a method to call at runtime.

由于在Objective-C中可以借助动态分派运行时更改方法调用,因此可以实现这一点。 这只不过是选择在运行时调用方法的哪个实现的过程。

Before we dive into the details of the implementation, it’s important to make some clarifications regarding the selectors.

在深入探讨实现细节之前,重要的一点是要对选择器进行澄清。

什么是选择器? (What is a selector?)

A selector represents the name used to select a method to execute for an object. A selector has no implementations and it does nothing on its own, its only purpose is to identify a method.

选择器代表用于选择要为对象执行的方法的名称。 选择器没有实现,它自己不执行任何操作,其唯一目的是识别方法

为什么要使用方法切换? (Why should I use method swizzling?)

There are many cases in which this process can be useful to you. In my case, it came in handy to detect the transition from one ViewController to another one within the app. In detail what I did was to change the selector of the viewWillAppear method in order to add some instructions to it at runtime.

在许多情况下,此过程可能对您有用。 就我而言,在应用程序中检测从一个ViewController到另一个ViewController的过渡非常方便。 详细地讲,我所做的是更改 viewWillAppear方法的选择器 ,以便在运行时向其添加一些指令。

让我们看看如何 (Let’s see how)

First things first you need to create an obj-c dynamic method:

首先,您需要创建一个obj-c动态方法

By applying the dynamic declaration modifier to a func, you tell the compiler that dynamic dispatch should be used to access that member.

通过将动态声明修饰符应用于func ,可以告诉编译器应使用动态分派来访问该成员。

The magic happens thanks to the method_exchangeImplementations method, its purpose is to exchange the implementation of two methods.

神奇的事情要归功于 method_exchangeImplementations 方法,其目的是交换两种方法的实现。

⚠️ It may seem that the _swizzled_viewWillAppear function above will result in an infinite loop. That’s not true!

⚠️ 似乎上面的_swizzled_viewWillAppear函数将导致无限循环。 这不是真的!

In the process of swizzling, _swizzled_viewWillAppear has been reassigned to the original implementation of viewWillAppear.

在处理过程中, _swizzled_viewWillAppear已重新分配给viewWillAppear的原始实现。

使它起作用 (Make it works)

I suggest you put your implementation into an extension:

我建议您将实现放入扩展中

Don’t forget to call the swizzle method when you want the exchange to happen.

当您希望进行交换时,请不要忘记调用swizzle方法。

Now, when any instance of UIViewController, or one of its subclasses invokes viewWillAppear, our _swizzled_viewWillAppear will be called.

现在,当UIViewController的任何实例或其子类之一调用viewWillAppear时 ,我们的_swizzled_viewWillAppear将被调用。

备注 (Remarks)

There are two requirements you must respect in order to use the swizzle method practice:

为了使用swizzle方法练习,必须满足两个要求:

  • Your class must extend NSObject.

    您的类必须扩展NSObject。

  • The functions you want to swizzle must have the dynamic attribute.

    要混淆的功能必须具有动态属性。

我想和你联系 (I want to hear from you)

As we have seen it is a powerful practice that can be implemented in various fields and contexts, I will be very happy to know how you will use it. :)

如我们所见,这是一种可以在各个领域和环境中实施的强大实践,我很高兴知道您将如何使用它。 :)

翻译自: https://medium.com/@m.delgiudice/swizzle-method-in-swift-8af664ffe77a

swizzle 特定方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值