I'm trying to declare a class which has a static variable which I want to set to an object of the class i.e.
class MyViewController: UIViewController {
@IBOutlet weak var title: UILabel!
static var staticSelf:MyViewController
This declaration generates the following error:
"class var declaration requires an initializer expression or getter/setter specifier".
So I tried several attempts at adding an initialzer but just was getting more or different compilation errors. So instead I tried adding a getter/setter specifier as it says in the error, but this is resulting in an infinite loop when run. I've tried several versions, this is the latest:
class MyViewController: UIViewController {
@IBOutlet weak var title: UILabel!
static var staticSelf:MyViewController {
set (selfInstance) {
MyViewController.staticSelf = selfInstance
}
get {
return MyViewController.staticSelf
}
}
override func viewDidLoad() {
super.viewDidLoad()
MyViewController.staticSelf = self
}
}
The initial attempt at the implementation of the setter method was staticSelf = selfInstance
, XCode flagged this as an error and auto-corrected to self.staticSelf = selfInstance
, but that results in an infinate loop, so I changed it to MyViewController.staticSelf = selfInstance
, but that too creates an infinate loop.
I've tried a thousand things to get this set up and could have done it in 3 seconds with objective-C , and am getting very fed up with Swift right now.
We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. A static member is shared by all objects of the class.
You create static variable by appending static keyword in front of your variable declaration. We will be using playground to explore more. When we define any variable as let, it means it's values cannot be modified, On the other hand if we define any variable as var it means it's values can be modified.
The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created.
how would you create an all static class in Swift? static means no instance, so I would make it a struct with no initializer: struct VectorCalculator { @available(*, unavailable) private init() {} static func dotProduct(v: Vector, w: Vector) -> Vector { ... } }
The basic example
class MyClass {
static var string: String
}
does not work because unitialized variables have nil
value. However, in Swift you can assign nil
only to optional variables.
You have two options:
Declare the variable as optional
static var string: String?
or
static var string: String? = nil
Initialize with a non-nil value:
static var string: String = "some value"
If your aim is to create a singleton, then see the official Apple guide for the solution:
class MyViewController: UIViewController {
static let staticSelf = MyViewController()
}
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