2016-05-06 8 views
0

Ich möchte Nib (Custom View eine Unterklasse von UIView) aber das Objekt sollte Singleton sein. Ich benutze unten Code für das gleiche auf Ziel C, aber jetzt schreibe ich Code in Swift 2.2loadNibNamed in Swift sollte zurück Singleton Objekt

Kann mir jemand helfen, diesen Code in Swift oder einem anderen Ansatz dafür zu konvertieren.

+(LoaderView *)sharedInstance; 
{ 
    static LoaderView * _sharedCommonInstance; 
    @synchronized(self) 
    { 
     if (!_sharedCommonInstance) 
     _sharedCommonInstance = [[[NSBundle mainBundle] loadNibNamed:@"LoaderView" owner:self options:nil] objectAtIndex:0]; 

     return _sharedCommonInstance; 
    } 
} 

Mein versuchen

class var sharedInstance: LoaderView { 
     struct Static { 
      static var onceToken: dispatch_once_t = 0 
      static var instance: LoaderView? = nil 
     } 
     dispatch_once(&Static.onceToken) { 
      Static.instance = NSBundle.mainBundle().loadNibNamed("LoaderView", owner: self, options: nil)[0] as? LoaderView 
     } 
     return Static.instance! 
    } 

aber durch diesen Ansatz App abstürzt, mein Screenshot Bitte überprüfen enter image description here

Vielen Dank im Voraus.

Antwort

0

Können Sie dies versuchen:

class LoaderView { 
static let sharedCommonInstance = NSBundle.mainBundle().loadNibNamed("LoaderView", owner: self, options: nil)[0] as UIView 

} 
0

gibt es verschiedene Ansätze. Sie können diese

import Foundation 
import UIKit 

class LoaderView: UIView { 

    class var sharedInstance: LoaderView { 
     struct Static { 
      static var onceToken: dispatch_once_t = 0 
      static var instance: LoaderView? = nil 
     } 
     dispatch_once(&Static.onceToken) { 
      Static.instance = NSBundle.mainBundle().loadNibNamed("LoaderView", owner: self, options: nil)[0] as? LoaderView 
     } 
     return Static.instance! 
    } 

    //approach 2 
    class var sharedInstanceA: LoaderView { 
     struct Static { 
      static let instance = NSBundle.mainBundle().loadNibNamed("LoaderView", owner: LoaderView.self, options: nil)[0] as! LoaderView 
     } 
     return Static.instance 
    } 

    //approach 3 
    static let sharedInstanceB = NSBundle.mainBundle().loadNibNamed("LoaderView", owner: LoaderView.self, options: nil)[0] as! LoaderView 

} 

versuchen Was ich in ViewDidLoad von einem Viewcontroller namens

override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     print(LoaderView.sharedInstance) 
     print(LoaderView.sharedInstanceA) 
     print(LoaderView.sharedInstanceB) 
} 

Die Konsole

gedruckt

output

Denken Sie daran setzen "LoaderView" Klasse in xib Datei

enter image description here

Sie können hier http://krakendev.io/blog/the-right-way-to-write-a-singleton

+0

ich diese alle Ansätze bereits versucht haben, verweisen aber nichts working.In 1 Ansatz einige Syntaxfehler gibt, und in Ansatz 2 und 3 selbst ist nicht zugänglich in struct –

+0

Könnten Sie die Post Syntaxfehler hier zur weiteren Untersuchung? – HDT

+0

UINib ( nibName: "Toast", Bündel: NSBundle.mainBundle() ) .instantiateWithOwner (self, Optionen: nil) [0] als? Toast Diese Zeile, die nil –