Is ClassName.staticVaribale
the only way to access static variable within the class? I want something like self
, but for class. Like class.staticVariable
.

- 1,779
- 3
- 17
- 23
-
1you can use: type(of: self).staticVaribale – soumil Jan 11 '20 at 10:06
-
@soumil Even better, if you have some unknown object (struct) that meets some protocol, and the protocol has a static variable/function, then your suggestion works perfectly! – David H Feb 24 '20 at 19:05
7 Answers
There are two ways to access a static property/method from a non-static property/method:
As stated in your question, you can prefix the property/method name with that of the type:
class MyClass { static let staticProperty = 0 func method() { print(MyClass.staticProperty) } }
Swift 2: You can use
dynamicType
:class MyClass { static let staticProperty = 0 func method() { print(self.dynamicType.staticProperty) } }
Swift 3: You can use
type(of:)
(thanks @Sea Coast of Tibet):class MyClass { static let staticProperty = 0 func method() { print(type(of: self).staticProperty) } }
If you're inside a static property/method you do not need to prefix the static property/method with anything:
class MyClass {
static let staticProperty = 0
static func staticMethod() {
print(staticProperty)
}
}

- 22,759
- 9
- 68
- 78
-
5So if we in the same class, but in instance method, we necessary need to write that class name? It just seems weird for me since compiler can definitely infer the class of instance. I just dont want to type the same class name. Would be great if I just can type like class.staticVaribale, but it is not the case? – simd Apr 29 '15 at 21:21
-
1I've updated my answer, sounds like you were looking to use `dynamicType`. – ABakerSmith Sep 27 '15 at 20:43
-
5
This is solved elegantly in Swift 5.1 you can access it via
Self.yourConstant
Reference: https://github.com/apple/swift-evolution/blob/master/proposals/0068-universal-self.md

- 2,135
- 4
- 23
- 29
There is a way in Swift to make Marcel's answer satisfy even most picky style-guide gods
class MyClass {
private typealias `Self` = MyClass
static let MyConst = 5
func printConst() {
print(Self.MyConst)
}
}
That makes Self available like in protocols when you want access associated type declaration. I am not sure about Swift 1 because never tried it but in Swift 2 it works perfectly

- 411
- 5
- 5
-
1Would be an ideal solution, if it could be more generic. Like some extension for all classes, or even for NSObject subclasses. But I think it's not possible to get dynamic class in extension statics. – simd Nov 01 '15 at 18:34
-
4This is the best solution. There is an [accepted proposal](https://github.com/apple/swift-evolution/blob/master/proposals/0068-universal-self.md) to add this to the language anyway. It would be subtly different in that `Self` would mean the dynamic type so if the member accessed was overridden it would use the overridden value. Basically this is the best solution as a stop-gap until this is officially part of the language. – jhabbott Jan 06 '17 at 14:52
-
1FWIW, this approach doesn't work for subclassed class functions. e.g. if you have `class func thing()`, `Self.thing()` will always call the parent class and never the sub classes. – GetSwifty Jul 18 '18 at 18:43
-
@Peej Since the type alias is explicitly declared as private, you shouldn't assume it works for subclasses – Eugene Berdnikov Nov 15 '19 at 06:26
-
1@EugeneBerdnikov right, but `printConst()` is not. Regardless, `Self` solves the OP issue anyway :) – GetSwifty Nov 15 '19 at 17:53
In a future Swift 3 version (yet to be released) you can use Self
(yes, that's with a capital) to reference to the containing class. A proposal for this was accepted, but the feature is not implemented yet.
For example:
struct CustomStruct {
static func staticMethod() { ... }
func instanceMethod() {
Self.staticMethod() // in the body of the type
}
}
Source: https://github.com/apple/swift-evolution/blob/master/proposals/0068-universal-self.md

- 6,339
- 5
- 50
- 77
-
@PEEJWEEJ hence my remark that it will be coming in Swift 3.1. I'm sure implementation will follow before 3.1 is released. – edwardmp Apr 26 '17 at 23:30
-
@PEEJWEEJ right, seems I am not up to date in this case. Then we'll have to wait but since it is accepted, it should be released in the near future. – edwardmp Apr 27 '17 at 17:14
-
You could work around this by defining a self referencing typealias.
class MyClassWithALongName {
typealias CLASS = MyClassWithALongName
static let staticFoo = "foo"
func someInstanceMethod() -> String {
return CLASS.staticFoo
}
}
Though the style-guide gods may not approve.

- 994
- 6
- 6
It looks like in Swift 4.2 the inner class and instance variable can directly access the static variable without the prefix of the class name. However, you still need the class name within a function.
class MyClass {
static let staticProperty = 0
let property = staticProperty //YES
class Inner {
func method() {
print(staticProperty) //YES
}
}
func method() {
print(staticProperty) //NO
print(MyClass.staticProperty) //YES
}
}

- 3,288
- 1
- 21
- 18
I don't like the typealias way in this case. My workaround is:
class MyClass {
static let myStaticConst: Int = 1
var myStaticConst:Int {
return type(of: self).myStaticConst
}
func method() {
let i:Int = myStaticConst
}
}

- 3,688
- 7
- 45
- 77