Swift 1.2 Singleton causes App to not launch

1.1k Views Asked by At

I have created a singleton for my iOS application to access certain things globally. When I launch the application int the simulator or on my iPhone/iPad however, it sticks on the launch screen, and never reaches the appdelegates didFinishLaunchingWithOptions method (I tried to println in it). If I remove the singleton and just leave the methods and variables as global it works perfectly. Which leaves me to believe it's the singleton causing this crash. Here's the code I use. If I comment out the lines currently commented out it works perfectly like this "gameName" anywhere in my code but I know this isn't great practice so if I uncomment them and access the singleton like this "Global.sharedInstance.gameName" is when the app does not launch. I do call this singleton many times throughout the app so I'm not sure if that's the issue.

//class Global {
//
//  static let sharedInstance = Global()
//  
//  private init() {
//      println("Global Singleton created");
//  }

private var optionsModel = OptionsModel()
private var gamesModel = GamesModel()
private var savesModel = SavesModel()

var device = (UIApplication.sharedApplication().delegate as! AppDelegate).device
var screenWidth = (UIApplication.sharedApplication().delegate as! AppDelegate).window!.bounds.width
var screenHeight = (UIApplication.sharedApplication().delegate as! AppDelegate).window!.bounds.height
var context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext!
var PI = CGFloat(M_PI)
var gameIndex = 0
var player: AudioPlayer!
var gameLevel = ""

func loadSong(name: String, loops: Int) {
    player = AudioPlayer(name: name, loopCount: loops)
}

func playAduio(fileName: String) {
    loadSong("\(fileName)Audio", 0)
    player!.play()
}

var gameName: String {
    get {
        return gamesModel.getName(gameIndex)
    }
}

var gameDescription: String {
    get {
        return gamesModel.getDescription(gameIndex)
    }
}

var gameIntervals: NSTimeInterval {
    get {
        return gamesModel.getIntervals(gameIndex)
    }
}

var gameNeedsMic: Bool {
    get {
        return gamesModel.getMic(gameIndex)
    }
}

var gameNeedsSpeech: Bool {
    get {
        return gamesModel.getSpeech(gameIndex)
    }
}

var appLocked: Bool {
    get {
        return optionsModel.appLocked
    }
    set {
        optionsModel.appLocked = newValue
    }
}

var supervisorLoggedIn: Bool {
    get {
        return optionsModel.supervisorLoggedIn
    }
    set {
        optionsModel.supervisorLoggedIn = newValue
    }
}

var themeSongMuted: Bool {
    get {
        return optionsModel.themeSongMuted
    }
    set {
        optionsModel.themeSongMuted = newValue
    }
}

var gameCount: Int {
    get {
        return optionsModel.gameCount
    }
    set {
        optionsModel.gameCount = newValue
    }
}

var gameHighscore: Int {
    get {
        return savesModel.getHighscore(gameIndex)
    }
    set {
        savesModel.setHighscore(newValue)
    }
}

var gameStarCount: Int {
    get {
        return savesModel.getStarCount(gameIndex)
    }
    set {
        savesModel.setStarCount(newValue)
    }
}
//}
1

There are 1 best solutions below

1
On

Silly me. I fixed it.

The problem was that (this is probably obvious to some of you but singletons are new to me) the singleton loaded before the app delegate loaded and some of the variables in the singleton tried to get information from the app delegate before it was created so it caused this crash. Changed it around so that i didn't load the information until after the app delegate was loaded and it works now.