0

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?

1
  • 2
    Can you show some code? as far as I know, there is not much of difference. Commented Nov 25, 2015 at 23:33

2 Answers 2

1

To me it sounds like you're not declaring your singleton correctly, more code would help. But for a quick solve, and just out of assumption - here is a quick easy way to declare a singleton with Swift 1.2

static let sharedInstance = Object()
@objc private override init() {} //This prevents others from using the default '()' initializer for this class

and then to use the singleton would be

Object.sharedInstance.fuctionName()
Sign up to request clarification or add additional context in comments.

Comments

0

In your original code snippet, xInstance is an instance attribute that needs to be initialized when an instance of X is created.

Static method getInstance() cannot access instance attributes or methods. (And hey! You're missing return type for the getInstance() method).

The easiest way to do a singleton with Swift these days is like this:

public class A {
    public static let sharedInstance = A()
}

And A.sharedInstance will be the singleton instance.

If you don't want others do A() manually outside this file, add a private init() {} line.

If you still prefer the X.getInstance() way, add a public static func getInstance() { return sharedInstance } line.

1 Comment

I still see nil value for xInstance when I fire the getInstance or even do X.xInstance. The line public static let sharedInstance = A() executes after the getInstance is called. Can you please tell me why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.