0

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"]);
}
5
  • You didn't post the code where your Data object is used, you just posted the class itself and a reference to the AppDelegate. Where do you create the Data object? Where do you use networkResponseHandler? Commented Oct 24, 2016 at 14:49
  • @MarcoPace I updated the question Commented Oct 24, 2016 at 14:58
  • Thanks for the update, but still some code missing: I can see that you perform the login after pressing a button, but I don't see where you ask for "result". Can you add the code where you try to access it? Commented Oct 24, 2016 at 15:03
  • @MarcoPace I added :) I tried to print data from result on console, but there s no output. Also, when i add some breakpoint within button handler there is no data in result. But when I add breakpoint in networkhandler there is data in result. Commented Oct 24, 2016 at 15:11
  • Ok clear, I'll add an answer :) Commented Oct 24, 2016 at 15:21

1 Answer 1

1

The problem is that you are performing an asynchronous request:

appDelegate.uData!.login();
print(appDelegate.uData!.result["result"]);

In this code you started an asynchronous request, it means that your print code is executed before you receive the data you want to print.

You need to implement your code after you receive the answer, in order to achieve that you need to add a delegate to your task and implement the correct method from NSURLSessionTaskDelegate.

Sign up to request clarification or add additional context in comments.

1 Comment

I get the problem. Thank you! My Data class should have this implementation from NSURLSessionTaskDelegate?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.