0

I'm an android app developer and a beginner in swift. I'm trying to implement a singleton class whose data members are shared throughout the app (like Settings). Getting this done in android is pretty simple but I'm breaking my head to do it in swift.

Below is the code I've tried ..

public class DataSet
{
    public var notificationOnOff: Bool!
    public var interval: Int!
    public var alert: String!

    init()
    {
        self.notificationOnOff = true
        self.interval = 1;
        self.alert = nil;
    }

    init (onOff: Bool) {
        self.notificationOnOff = onOff
    }

    init (time: Int) {
        self.interval = time
    }

    init (stop: String) {
        self.alert = stop
    }
}

This class implementation couldn't persist the data. Is this the right way of doing it?

EDIT

For example, when I click switch in Settings view controller, I'm setting notificationOnOff like ..

dataset.notificationOnOff = DataSet(onOff: true) // value is set and able to print it

and using this value in another class like...

if dataset.notificationOnOff
{
 // do some stuff
}

So basically, the value is persisting only within the Setting class but not when I switch to other class.

Srujan Simha
  • 3,637
  • 8
  • 42
  • 59
  • 1
    References: 1--http://stackoverflow.com/questions/24024549/dispatch-once-singleton-model-in-swift 2--http://code.martinrue.com/posts/the-singleton-pattern-in-swift – Mrunal Dec 17 '14 at 17:10
  • Did you try to use struct instead of class ? – Amr Hossam Dec 17 '14 at 17:13
  • @Mrunal Yes I've checked that before. I kind of couldn't understand that implementation. He used `static var instance: Singleton?` but how do we use int or string data members in the class to share their values? – Srujan Simha Dec 17 '14 at 17:13
  • @AmroShafie Yes but that didn't work as well. – Srujan Simha Dec 17 '14 at 17:15
  • Try removing public while using struct. – Amr Hossam Dec 17 '14 at 17:18
  • @AmroShafie Still not working. – Srujan Simha Dec 17 '14 at 17:20
  • So you init DataSet by using DataSet(1) for example, then trying to recall notificationOnOff by using DataSet.notificationOnOff outputs 0 ? – Amr Hossam Dec 17 '14 at 17:26
  • @AmroShafie Exactly. I edited my post. Check it! – Srujan Simha Dec 17 '14 at 17:36
  • Ok, try initializing your struct in your Appdelegate's didFinishLaunchingWithOptions method to make sure it's loaded through all application views like this DataSet(). Just don't store it in a variable. Then in another view try to set/get values like so, DataSet(1) and log DataSet.notificationOnOff, it should print 1, assuming you are using struct not class. – Amr Hossam Dec 17 '14 at 17:41
  • @AmroShafie Sorry again, that's not working either. I did just like the way you said. I'm abel to print the value after clicking switch but when I exit from the Settings class its gone. – Srujan Simha Dec 17 '14 at 17:50

1 Answers1

0

Solved!

I have used the below code to successfully implement this..

var instance: DataSet?
class Dataset {
...
    class var sharedInstance: DataSet {
        struct Static {
            static var instance: DataSet?
            static var token: dispatch_once_t = 0
        }

        dispatch_once(&Static.token) {
            Static.instance = DataSet()
        }

        return Static.instance!
    }
}

and in the other classes ..

dataset = Dataset.sharedInstance;
Srujan Simha
  • 3,637
  • 8
  • 42
  • 59