闽公网安备 35020302035485号
import UIKit
class ViewController: UIViewController {
// 堆代码 duidaima.com
override func viewDidLoad() {
super.viewDidLoad()
let someClass = SomeClass()
someClass.originalMethod()
}
}
class SomeClass {
@objc dynamic func originalMethod() {
print("I am the original method")
}
}
运行以上代码,我们会看到 originalMethod 的输出:I am the original method现在,我们将添加一些代码以将 originalMethod 的实现替换为 swizzledOriginalMethod 的实现:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let selector1 = #selector(SomeClass.originalMethod)
let selector2 = #selector(SomeClass.swizzledOriginalMethod)
let method1 = class_getInstanceMethod(SomeClass.self, selector1)!
let method2 = class_getInstanceMethod(SomeClass.self, selector2)!
method_exchangeImplementations(method1, method2)
let someClass = SomeClass()
someClass.originalMethod()
}
}
class SomeClass {
@objc dynamic func originalMethod() {
print("I am the original method")
}
}
extension SomeClass {
@objc func swizzledOriginalMethod() {
print("I am the replacement method")
}
}
运行以上代码,我们可以确认在调用 originalMethod 时,实际上调用的是 swizzledOriginalMethod:I am the replacement method现代 swift 方法
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let someClass = SomeClass()
someClass.originalMethod()
someClass.orignalMethodWithParameters(for: "Param1", param2: "Param2")
print(someClass.originalMethodWithParametersAndReturnType(param: "Param"))
}
}
class SomeClass {
dynamic func originalMethod() {
print("I am the original method")
}
dynamic func orignalMethodWithParameters(for param1: String, param2: String) {
print("I am the original method with parameters - param1: \(param1) - param2: \(param2)")
}
dynamic func originalMethodWithParametersAndReturnType(param: String) -> Bool {
print("I am the original method with parameter - param: \(param) - and return type: Bool")
return true
}
}
当运行上述代码时,我们将看到以下输出:I am the original method I am the original method with parameters - param1: Param1 - param2: Param2 I am the original method with parameter - param: Param - and return type: Bool true现在,添加以下代码以 @_dynamicReplacement 注解来用新实现替换原始方法:
extension SomeClass {
@_dynamicReplacement(for: originalMethod)
func replacementMethod() {
print("I'm the replacement method")
}
@_dynamicReplacement(for: orignalMethodWithParameters(for:param2:))
func replacementMethodWithParameters(param1: String, param2: String) {
print("I am the replacement method with parameters - param1: \(param1) - param2: \(param2)")
}
@_dynamicReplacement(for: originalMethodWithParametersAndReturnType(param:))
func replacement_withParameters_andReturnType(param: String) -> Bool {
print("I am the replacement method with parameter - param: \(param) - and return type: Bool")
return false
}
}
再次运行应用程序,我们将在控制台中看到以下输出:I'm the replacement method I am the replacement method with parameters - param1: Param1 - param2: Param2 I am the replacement method with parameter - param: Param - and return type: Bool false如上所示,扩展中的方法实现已经替换了 SomeClass 的原始方法实现,而无需我们对子类进行子类化。
.清晰地记录和注释交换的方法。