Moises Zermeño Moises Zermeño - 1 year ago 128
Swift Question

Is Swift a dynamic or static language?

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

Answer Source

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.