i wrote a 2 classes in my app that need to perform as data classes . one is for the user details and one is for the products. both of the classes is the same code with different names and variables. the products class works ok but the user class don't make the sharedInstance for me. the Json get the data ok and i can print it.
here is my 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 to 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 from alamo ")
}
}
let nc = NSNotificationCenter.defaultCenter()
nc.postNotificationName("userDataReadyForSplash", object: nil)
}
}
//Create one class for 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!
}
}