-2

My problem is i've constants class which is used in almost each classes in application so i've created its object in AppDelegate n through it want to access them.i don't want create object of constants every where each time.what will be the best approach please advice.

class AppDelegate: UIResponder, UIApplicationDelegate {
 public var constants = Constants()
//created object once in app delegate
}

class Utility
{
let delegate = UIApplication.shared.delegate as! AppDelegate //getting     null here
let constants =  delegate. constants
}`
Nitisha Sharma
  • 239
  • 4
  • 14
  • 2
    `UIApplicationDelegate` already an unique instance. Do not ever create its copy. Just use its reference. – Blind Ninja Nov 23 '16 at 11:25
  • 5
    Possible duplicate of [How do I get a reference to the app delegate in Swift?](http://stackoverflow.com/questions/24046164/how-do-i-get-a-reference-to-the-app-delegate-in-swift) – Vinodh Nov 23 '16 at 11:25
  • Search SO before adding duplicate questions – Vinodh Nov 23 '16 at 11:27
  • @Vinodh - My problem is i've constants class which is used in almost each classes in application so i've created its object in AppDelegate n through it want to access them.i don't want create object of constants every where each time.what will be the best approach please advice. class AppDelegate: UIResponder, UIApplicationDelegate { public var constants = Constants() //created object once in app delegate } class Utility { let delegate = UIApplication.shared.delegate as! AppDelegate //getting null here let constants = delegate. constants } – Nitisha Sharma Nov 23 '16 at 11:49
  • i know but the above code is not good way to create constant. If you dealing Swift create struct and declare all yours constants there please use SO post http://stackoverflow.com/a/26252377/1142743 for more details – Vinodh Nov 23 '16 at 11:55

2 Answers2

1

This should work to create an object to AppDelegate:

let appDelegate = UIApplication.shared.delegate as! AppDelegate

You may want to create a singleton class instead.

class MySingleton : NSObject
{
  class var sharedInstance :MySingleton
  {
    struct Singleton
    {
        static let instance = MySingleton()
    }

    return Singleton.instance
  }

  final let myVar = "String"
}

And then access the properties by creating an object:

let appSingleton = MySingleton.sharedInstance
appSingleton.myVar

Is this what you are trying to do?

Igor Tupitsyn
  • 1,193
  • 3
  • 18
  • 45
  • @NitishaSharma please check my answer because you should probably refrain from using a singleton for your usecase – christopher.online Nov 24 '16 at 13:55
  • @igor I think this is a better way to create a singlton if you want to use one: `final class Singleton { static let sharedInstance: Singleton = { let instance = Singleton() return instance }() private init() { //make it private so nobody can initialise the Singleton from outside } }` – christopher.online Nov 24 '16 at 13:59
  • @igor even shorter: `final class Singleton { static let sharedInstance = Singleton() private init() { //make it private so nobody can initialise the Singleton from outside } }` – christopher.online Nov 24 '16 at 15:31
  • @KaraBenNemsi. Perhaps, you are right. I used some reference material on how to create singletons in Swift and the above code works fine in my application. But I am not arguing, may be there are other (better) ways to create them. Cheers! – Igor Tupitsyn Nov 25 '16 at 01:02
0

You should probably not use a singleton because you are just using the class for strings. Instead you should probably go for something like this:

struct LocalizationConstants {
    static let settings = NSLocalizedString("Settings", comment: "")
    static let main = NSLocalizedString("Main", comment: "")
    static let someString = "value"
}
christopher.online
  • 2,614
  • 3
  • 28
  • 52