couple of singletons in one swift 2 application -


i wrote 2 classes in app need perform data classes . 1 user details , 1 products. both of classes same code different names , variables. products class works ok user class don't make sharedinstance me. json data ok , can print it.

here code:

import uikit import alamofire import swiftyjson  class userdetailsdataclass {     let defaults = nsuserdefaults.standarduserdefaults()     var user: [swiftyjson.json] = []        init() {         let baseurlstring =  "http://example.com/ios/login.php?"         let parameters = [string:string]()          let urlsignup = baseurlstring + "id=" + defaults.stringforkey("userid")! + "&macnumber=" + defaults.stringforkey("currentuuid")!          //encode url solve spaces issues         let urlwithpercentescapes = urlsignup.stringbyaddingpercentencodingwithallowedcharacters( nscharacterset.urlqueryallowedcharacterset())!         print(urlwithpercentescapes)         alamofire.request(.get, urlwithpercentescapes, parameters: parameters).responsejson { response in             if response.result.issuccess {                 let json = json(response.result.value!)                 let data = json.arrayvalue                 print(json)                   if json["error"] == "wrongmac" {                     print("wrong mac")                  } else {                      let macfromdb = json["macnumber"]                     self.defaults.setvalue(string(macfromdb), forkey: "macfromdb")                    userdetailsdataclass.sharedinstance.user = data                     print(string(userdetailsdataclass.sharedinstance.user.count)+"user count alamo ")                   }              }              let nc = nsnotificationcenter.defaultcenter()            nc.postnotificationname("userdatareadyforsplash", object: nil)           }       }        //create 1 class shared instance     struct static {         static var oncetoken : dispatch_once_t = 0         static var instance : userdetailsdataclass? = nil     }      class var sharedinstance : userdetailsdataclass {         dispatch_once(&static.oncetoken){             static.instance = userdetailsdataclass()         }         return static.instance!     } } 

the singleton should defined directly in class , not subclass (and please put uppercase first character in class name):

class userdetailsdataclass {     static let sharedinstance = userdetailsdataclass()      let defaults = nsuserdefaults.standarduserdefaults()     var user: [swiftyjson.json] = []       init() {         let baseurlstring =  "http://example.com/ios/login.php?"         let parameters = [string:string]()          let urlsignup = baseurlstring + "id=" + defaults.stringforkey("userid")! + "&macnumber=" + defaults.stringforkey("currentuuid")!          //encode url solve spaces issues         let urlwithpercentescapes = urlsignup.stringbyaddingpercentencodingwithallowedcharacters( nscharacterset.urlqueryallowedcharacterset())!         print(urlwithpercentescapes)         alamofire.request(.get, urlwithpercentescapes, parameters: parameters).responsejson { response in             if response.result.issuccess {                 let json = json(response.result.value!)                 let data = json.arrayvalue                 print(json)                   if json["error"] == "wrongmac" {                     print("wrong mac")                  } else {                      let macfromdb = json["macnumber"]                     self.defaults.setvalue(string(macfromdb), forkey: "macfromdb")                      userdetailsdataclass.sharedinstance.user = data                      // addition: try print whole swiftyjson array , see result                     print(userdetailsdataclass.sharedinstance.user)                      print(string(userdetailsdataclass.sharedinstance.user.count)+"user count alamo ")                 }             }              let nc = nsnotificationcenter.defaultcenter()             nc.postnotificationname("userdatareadyforsplash", object: nil)         }     } } 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -