Exceptions Exceptions - 5 months ago 29
Swift Question

Accessing method in another Swift file in AppDelegate

Here is my settings.swift file :

import Foundation

class Settings {

func setDefaultSettings() {
}
}


I would like to access the function in AppDelegate
didFinishLaunchingWithOptions
. I've tried calling

Settings.setDefaultSettings()
but it didn't work.

Answer

You have two ways to make this work:

1.Initialise object of Settings class

Settings().setDefaultSettings()   

or

let settings = Settings()
settings.setDefaultSettings()

2.Make the function static

static func setDefaultSettings(){...}

in this case you can call it your way

Settings.setDefaultSettings()
Comments