I'm just curious if Swift is dynamic like php or static, I mean can I generate classes while an application is running?
Swift is a statically typed language, meaning that the type of every property, constant and variable that we declare needs to be specified at compile time.
Statically typed languages perform type checking at compile-time, while dynamically-typed languages perform type checking at run-time. Statically-typed languages require you to declare the data types of your variables before you use them, while dynamically-typed languages do not.
Objective-C uses the id data type to represent a variable that is an object without specifying what sort of object it is. This is referred to as dynamic typing. Dynamic typing contrasts with static typing, in which the system explicitly identifies the class to which an object belongs at compile time.
Ruby is a dynamic language, which means that types are checked when the code is run. If you try to call a method on an object that does not exist, the compiler will not complain, you'll only find out about that error when the code is executed and you get a NoMethodError .
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.
Swift itself, is statically typed. When used with Cocoa, you get access to the objective-c runtime library which gives you the ability to use dynamic classes, messages and all. This doesn't mean the language itself is dynamically typed. You could do the same with C or any other language that supports a bridge to C by using libobjc.A.dylib
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With