Access Static Variables Within Class in Swift

Access static variables within class in Swift

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

Access static variable from non static method in Swift

You need to include the class name before the variable.

class MyClass {

static var myArr = [String]()

func getArr() -> [String] {
return MyClass.myArr
}
}

Accessing a static variable and setting it outside the class

The error message is trying to tell you that you are referring to a type property (static property) as if it was an instance property. You need to preface "CellIdentifier" with "SubtitleCustomField" in your SubtitleCustomField initializer just like you do when referring to it elsewhere.

override init(frame: CGRect) {
super.init(frame: frame)
if SubtitleCustomField.CellIdentifier == "A" {
//DO SOMETHINIG
} else if SubtitleCustomField.CellIdentifier == "B" {
//DO SOMETHING
}
}

You should always refer to type properties using the type name followed by "." followed by the property name.

How to access static variables in methods using Swift's 'abstract class'-like protocol extensions

You should use numSide as a static variable not instance one.
You cannot call Shape.numSides but you can use Self keyword which reference to the concrete class.
Try this:

Self.numSides

How to reference static Swift Struct or class variables without using the Type name?

There are three ways:

    MyStruct.variable
type(of:self).variable
Self.variable

The Self keyword is a relatively recent Swift innovation, and is probably your preferred choice here. The advantage of type(of:) and Self over just saying the name is that they are polymorphic.

When to use static constant and variable in Swift?

When you define a static var/let into a class (or struct), that information will be shared among all the instances (or values).

Sharing information

class Animal {
static var nums = 0

init() {
Animal.nums += 1
}
}

let dog = Animal()
Animal.nums // 1
let cat = Animal()
Animal.nums // 2

As you can see here, I created 2 separate instances of Animal but both do share the same static variable nums.

Singleton

Often a static constant is used to adopt the Singleton pattern. In this case we want no more than 1 instance of a class to be allocated.
To do that we save the reference to the shared instance inside a constant and we do hide the initializer.

class Singleton {
static let sharedInstance = Singleton()

private init() { }

func doSomething() { }
}

Now when we need the Singleton instance we write

Singleton.sharedInstance.doSomething()
Singleton.sharedInstance.doSomething()
Singleton.sharedInstance.doSomething()

This approach does allow us to use always the same instance, even in different points of the app.

Accessing non static constant in static function of swift class

No, there isn't a way to access non-static variables in static functions. You can create a static instance of the class (a way a singleton is made) and access its testString variable, though.



Related Topics



Leave a reply



Submit