Ich habe Probleme mit dispatch_once_t
wenn Swift Migration 3.'dispatch_once_t' ist in Swift nicht verfügbar: lazily initialisiert Globals Verwenden Sie stattdessen
Nach Apple's migration guide:
The free function dispatch_once is no longer available in Swift. In Swift, you can use lazily initialized globals or static properties and get the same thread-safety and called-once guarantees as dispatch_once provided. Example:
let myGlobal = { … global contains initialization in a call to a closure … }()
_ = myGlobal // using myGlobal will invoke the initialization code only the first time it is used.
Also wollte ich diesen Code migrieren . So war es vor der Migration:
class var sharedInstance: CarsConfigurator
{
struct Static {
static var instance: CarsConfigurator?
static var token: dispatch_once_t = 0
}
dispatch_once(&Static.token) {
Static.instance = CarsConfigurator()
}
return Static.instance!
}
Nach der Migration im Anschluss an die Apple-Richtlinien (manuelle Migration), sieht der Code wie folgt aus:
class var sharedInstance: CarsConfigurator
{
struct Static {
static var instance: CarsConfigurator?
static var token = {0}()
}
_ = Static.token
return Static.instance!
}
Aber wenn ich diese laufen bekomme ich folgende Fehlermeldung, wenn Zugriff auf return Static.instance!
:
fatal error: unexpectedly found nil while unwrapping an Optional value
ich von diesem Fehler zu sehen, dass das instance
Mitglied ist nil
, aber warum ist es? Ist mit meiner Migration etwas nicht in Ordnung?
'dispatch_once' entfernt wird in' Swift 3'. Überprüfen Sie [this] (http://stackoverflow.com/a/37801408/5654848), wie Sie die Dinge "einmal" tun können. – Dershowitz123