0

I have app delegate which is a single point in app. Also I've created ApplicationManager class which is a part of app delegate now.

so what I want to achieve is to protect my code from other developers to be used in a wrong way.

So let's say my ApplicationManager looks like this:

class ApplicationManager {

    var api: API?

    static func instance() -> ApplicationManager {
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
            fatalError()
        }
        return appDelegate.applicationManager
    }
}

I want to make sure that used will use ApplicationManager via:

let am = ApplicationManager.instance() but not like this am = ApplicationManager() which will create one more manager which I don't want.

is there a way to show build time error? or drop some message? or crash in crash in case there are more that one ApplicationManager in app =)

0

1 Answer 1

4

Simply make your init private:

private init() {}

Also, the singleton pattern would be usually implemented simply as:

static let shared = ApplicationManager()

Called as ApplicationManager.shared. If you want AppDelegate to create the instance, private init won't work.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.