Is Swift a dynamic or static language?

前端 未结 4 561
生来不讨喜
生来不讨喜 2020-12-15 03:23

I\'m just curious if Swift is dynamic like php or static, I mean can I generate classes while an application is running?

4条回答
  •  青春惊慌失措
    2020-12-15 04:08

    It is static - very static. The compiler must have all information about all classes and functions at compile time. You can "extend" an existing class (with an extension), but even then you must define completely at compile time what that extension consists of.

    Objective-C is dynamic, and since, in real life, you will probably be using Swift in the presence of Cocoa, you can use the Objective-C runtime to inject / swizzle methods in a Swift class that is exposed to Objective-C. You can do this even though you are speaking in the Swift language. But Swift itself is static, and in fact is explicitly designed in order to minimize or eliminate the use of Objective-C-type dynamism.

提交回复
热议问题