How to use singletons in Swift for data sharing? -
i'm working on app requires information shared between multiple views. simplicity's sake, prefer use singleton said purpose.
my questions are...
- where should singleton class defined , initialized available view controllers , other class files?
- how should other classes reference said singleton? import statements or special initializations within specific class necessary?
i'd mention i'm intrigued 1 line singleton found @ http://krakendev.io/blog/the-right-way-to-write-a-singleton
such method preferable.
btw: i'm developing using swift in xcode 6.2
thanks.
if define singleton, it's available everywhere in target. put wherever want, own .swift
file. , use it, you'd following, no special import
statements needed:
let kraken = theoneandonlykraken.sharedinstance
btw, if you're using xcode 6.2, predates swift 1.2 introduced xcode 6.3, you're limited of less elegant struct
solutions in article reference.
for record, using singletons gain access model data throughout app intoxicatingly simple, rmaddy suggested, practice discouraged. apps more complicated, starts introduce subtle maintenance issues. it's not habit adopt. is, admittedly, subject of debate, instead refer what bad singletons?, rather starting conversation again here.
Comments
Post a Comment