overriding static vars in subclasses swift 1.2
The documentation says:
“
static
” methods and properties are now allowed in classes (as an alias for “
class final
”).
So it is final
, which means you cannot override it.
Struct static vars under inheritance
The program (unsurprisingly) can't compile because it found two candidates for the SOMECONST
symbol:
error: ambiguous use of 'SOMECONST'
super.init(val: B.Static.SOMECONST)
^
note: found this candidate
static let SOMECONST = 2
^
note: found this candidate
static let SOMECONST = 1
^
Using nested types for storing constants like this is generally a bad idea, because nested types can't be overriden by subclasses. You should declare the constants directly as part of your class.
Now, ideally you would do something like this:
class A {
class let SOMECONST = 1
}
class B: A {
override class let SOMECONST = 2
}
But, unfortunately, this is not supported by the current Swift compiler. The only way you can override a class (static) variable is to make it computable:
class A {
class var SOMECONST: Int {
return 1
}
}
class B: A {
override class var SOMECONST: Int {
return 2
}
}
That's a little bit uglier, but it works. You can now create your B
's initializer:
init() {
super.init(val: B.SOMECONST) // prints "2"
}
Static vs class functions/variables in Swift classes?
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.
Previously written code, developer made a static var that returns a block?
The "block" isn't "returned". The closure is defined, and executed immediately, returning .Develop
, which is saved to current
.
This is useful when you need something to occur during initialization of the variable, but in this, static var current: Environment = .Develop
is sufficient, and preferable.
Does swift have class level static variables?
Swift supports static type properties, including on classes, as of Swift 1.2:
class MyClass {
static let pi = 3.1415926
}
If you need to have a class variable that is overridable in a subclass, you'll need to use a computed class property:
class MyClass {
class var pi: Double { return 3.1415926 }
}
class IndianaClass : MyClass {
override class var pi: Double { return 4 / (5 / 4) }
}
static vs class as class variable/method (Swift)
From the Xcode 3 beta 3 release notes:
“static” methods and properties are now allowed in classes (as an
alias for “class final”).
So in Swift 1.2, hi()
defined as
class foo {
static func hi() {
println("hi")
}
}
is a type method (i.e. a method that is called on the type itself)
which also is final (i.e. cannot be overridden in a subclass).
Circular warnings about Swift static override being final
Thanks to Martin R above for the link to the issue in the Swift compiler. That issue also has a workaround, that does indeed fix the issue for me.
Fixing this is possible by actually using class instead of static in the override in class Y.
Related Topics
Take Screenshot of Host App Using iOS Share/Action Extensions
Fail to Import Restkit with Cocoapods Dynamic Frameworks
Sort Dictionary Keys by Value, Then by Key
Referencing Self in Super.Init
Characters Is Unavailable' Please Use String Directly
How to Prompt for Accessibility Features in a MACos App (From the Appdelegate)
Instance Member Cannot Be Used on Type | Closures
Does Nsnumberformatter.Stringfromnumber Ever Return Nil
Explicitly Unwrapping Optional Nil Does Not Cause Crash
Using "Codable" to Set Property Values Doesn't Work Through Inheritance
Convert String to Nsdate in Swift
How to Make Xcode Put Starting Brace on New Line in Swift
Swift 4.2 - _Shared Attribute Near Type
Images Inaccessible from Asset Catalog in a Swiftui Framework
How to Reduce the Opacity of the Shadows in Realitykit
Guard Let Error: Initializer for Conditional Binding Must Have Optional Type Not 'String'
Swift 4: Validating Credit Card Expiration Date
Difficulties to Assign Default Value to a Parameter of a Function