The following code compiles in Swift 1.2:
class myClass { static func myMethod1() { } class func myMethod2() { } static var myVar1 = "" } func doSomething() { myClass.myMethod1() myClass.myMethod2() myClass.myVar1 = "abc" }
What is the difference between a static function and a class function? Which one should I use, and when?
If I try to define another variable class var myVar2 = ""
, it says:
Class stored properties not yet supported in classes; did you mean 'static'?
When this feature is supported, what will the difference be between a static variable and a class variable (i.e. when both are defined in a class)? Which one should I use, and when?
(Xcode 6.3)
Class functions are dynamically dispatched and can be overridden by subclasses. Static functions are invoked by a class, rather than an instance of a class. These cannot be overridden by a subclass.
The main difference is static is for static functions of structs and enums, and class for classes and protocols. From the Chris Lattner the father of Swift. We considered unifying the syntax (e.g. using "type" as the keyword), but that doesn't actually simply things.
Swift allows us to use a static prefix on methods and properties to associate them with the type that they're declared on rather than the instance. We can also use static properties to create singletons of our objects which, as you have probably heard before is a huge anti-pattern.
Static variables are those variables whose values are shared among all the instance or object of a class. When we define any variable as static, it gets attached to a class rather than an object. The memory for the static variable will be allocation during the class loading time.
static
and class
both associate a method with a class, rather than an instance of a class. The difference is that subclasses can override class
methods; they cannot override static
methods.
class
properties will theoretically function in the same way (subclasses can override them), but they're not possible in Swift yet.
I tried mipadi's answer and comments on playground. And thought of sharing it. Here you go. I think mipadi's answer should be mark as accepted.
class A{ class func classFunction(){ } static func staticFunction(){ } class func classFunctionToBeMakeFinalInImmediateSubclass(){ } } class B: A { override class func classFunction(){ } //Compile Error. Class method overrides a 'final' class method override static func staticFunction(){ } //Let's avoid the function called 'classFunctionToBeMakeFinalInImmediateSubclass' being overriden by subclasses /* First way of doing it override static func classFunctionToBeMakeFinalInImmediateSubclass(){ } */ // Second way of doing the same override final class func classFunctionToBeMakeFinalInImmediateSubclass(){ } //To use static or final class is choice of style. //As mipadi suggests I would use. static at super class. and final class to cut off further overrides by a subclass } class C: B{ //Compile Error. Class method overrides a 'final' class method override static func classFunctionToBeMakeFinalInImmediateSubclass(){ } }
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