Can anybody explain me the difference between static variables in swift vs static variables in java?
I know that the static functions in java execute while the memory gets allocated and not when the instance is created for an object.
I was trying to make a singleton in swift, I saw that the function call for getInstance() in my ViewController viewDidLoad function was executing before my static variables get populated and therefore I am returning nil values.
How do I approach this problem? Should I find an "Initializer" sort of a function where all the allocations and initializations for classes happen so this works smoothly?
Please advise
I am adding a code snippet for the same
class X {
var xInstance:X = X()
public static func getInstance() { return xInstance }
}
class Y {
var xInstance: X?
var yInstance: Y = Y()
public override init() {
xInstance = X.getInstance()
public static func getInstance() {
return yInstance
}
}
class someView: UIView {
public override init() {
var xInstance = X.getInstance() //This is my problem area. I am seeing nil for xInstance
}
}
I am seeing nil values for xInstance. What am I doing wrong?