I have one Data class with variable of NSDictionary type
class Data {
var user : String;
var pass : String;
var result: NSDictionary;
init(user: String, pass: String) {
self.user = user;
self.pass = pass;
self.result = NSDictionary();
}
func networkResponseHandler(data: Foundation.Data?, response:URLResponse?, err:Error? ) -> Void {
if err != nil { print(err); return; }
let jsonResult : NSDictionary = try! JSONSerialization.jsonObject(with: data! as Foundation.Data, options: .allowFragments) as! NSDictionary;
//print(jsonResult);
print(jsonResult["result"])
self.result = jsonResult.copy() as! NSDictionary;
}
func login() -> Bool {
let url = URL(string: "http://");
let task = URLSession.shared.dataTask(with: url!, completionHandler: networkResponseHandler );
task.resume();
return true;
}
}
The instance of this class is in AppDelegate file, and im accesing it like this :
let appDelegate = UIApplication.shared.delegate as! AppDelegate
But, in function networkResponseHandler when I use breakpoints, I see result has proper values, but outside the function it is empty.
Use of Data object: ViewControler.swift
@IBAction func bSignInPressed(_ sender: AnyObject) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.uData = Data(user: tEmail.text!, pass: tPassword.text!);
appDelegate.uData!.login();
print(appDelegate.uData!.result["result"]);
}