3

As we knew, when an Objective-C class was loaded the +load method would be called. But in Swift we don't have +load method.

So I tried to invested this. I added a symbol breakpoint at call_load_methods. In Objective-C world. this function will call all the class's +load. But it didn't enter this breakpoint.

So I wondering how to load in Swift. Or can you guys give me more detail a about Swift runtime or something else?

billwang1990
  • 627
  • 2
  • 6
  • 16
  • 1
    More details is: `Swift` doesn't have runtime. Welcome to `C++` club. What you trying to achieve? Could you be more specific? – Cy-4AH Aug 19 '16 at 10:35

1 Answers1

0

Unfortunately, a load class method implemented in Swift is never called by the runtime, rendering that recommendation an impossibility. Instead, we're left to pick among second-choice options:

Implement method swizzling in initialize. This can be done safely, so long as you check the type at execution time and wrap the swizzling in dispatch_once (which you should be doing anyway). Implement method swizzling in the app delegate. Instead of adding method swizzling via a class extension, simply add a method to the app delegate to be executed when application(_:didFinishLaunchingWithOptions:) is called. Depending on the classes you're modifying, this may be sufficient and should guarantee your code is executed every time.

source

Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49
  • I know swift would not call load method and how to do swizzling in Swift. I want to load how swift load a class. For example, in objc runtime, the `call_load_methods` will be called, then `call_class_loads` and `call_category_loads`. But the swift runtime is different, I want to know how different it is? How to load a class image in swift, what's the swift runtime look like ? – billwang1990 Aug 22 '16 at 03:52