You will get this error if your class satisfies these conditions.
- Subclassed from
NSObject
.
- Has a
static let
field.
- Accesses the field from an instance method via
dynamicType
.
I don't know why this happens, but you can try this workaround.
static var value: Int {
get {
return 111
}
}
Or in shorter form.
static var value: Int {
return 111
}
Use static var { get }
instead of static let
.
Though property getter and its calling cost is very likely to be eliminated by LLVM optimiser in above example, you might want to avoid it explicitly.
If you're concerned about such value calculation cost, you can create it once and cache like this.
static var value: Int {
return cache
}
private let cache = getTheNumber()
Or like this if you want to hide the existence of cache completely.
static var value: Int {
struct Local {
static let cache = getTheNumber()
}
return Local.cache
}
func test2
declaration is not needed to trigger the error, as of Xcode 7.3.1.