Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method Swizzling #85

Closed
MoSonLee opened this issue Oct 16, 2022 · 0 comments
Closed

Method Swizzling #85

MoSonLee opened this issue Oct 16, 2022 · 0 comments

Comments

@MoSonLee
Copy link
Owner

Method swizzling

  • 런타임 시점에 기존 메서드를 바꾸어 실행하는 것을 말하며 objective-c의 런타임 기능입니다.
  • 예를 들어, 앱의 생명주기에 앱의 분석 기능을 통합하거나, 특정 클래스의 기능을 한번에 적용하고 할 때 사용함
  • 하지만 런타임에서 메서드가 바뀌기 때문에 iOS버전에 따라 동작하지 않을 수도 있고 런타임에서 메서드의 오류가 발생할 수 있음.
  • 또는 Firebase 등과 같은 툴에서 이미 Method Swizzling을 하고 있을 수도 있기 때문에 미리 체크해야함
extension UIViewController {
    
    class func swizzleMethod() {
        
        let origin = #selector(viewWillAppear)
        let change = #selector(changeViewWillAppear)
        
        guard let originMethhod = class_getInstanceMethod(UIViewController.self, origin), let changeMethod = class_getInstanceMethod(UIViewController.self, change) else {
            print("함수를 찾을 수 없거나 오류 발생")
            return
        }
        
        method_exchangeImplementations(originMethhod, changeMethod)
    }
    
    @objc func changeViewWillAppear() {
        print("Change ViewWillAppear SUCCEED")
    }
}
  • Appdelegate의 didFinishLaunchingWithOptions UIViewController.swizzleMethod()를 추가해주면 됨
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant