Parse JSON return nil iOS Swift

0 votes

Hi the below is a snippet of my code

var wishList = App42API.buildStorageService() as? StorageService

var json: [String: AnyObject]!

    init() {

  wishList?.findAllDocuments(dataBase, collectionName: collectionName, completionBlock: {(success, response, exception) -> Void in

            if(success){

                let storage = response as! Storage

                let jsonDocList = storage.jsonDocArray

                var jsonString: String {

                    var someString: String = ""

                    for jsonDoc in jsonDocList{

                        someString = jsonDoc.jsonDoc

                    }

                    return someString

                }

                let data = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!

                do {

                    self.json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) as! [String: AnyObject]

                }catch{

                    print(error)

                }

                print(self.json)

            }else{

                        NSLog("%@", exception.reason!   }})}})}

func printJSON(){

        if json != nil{

        print(json)

        }else{

            print("error") //Always return nil: error

        }

    }

The variable json only has value when it's in the if(success) statement. Even after it has been successfuly executed in the if(success) statement and I try to use the variable json elsewhere, it always returns nil.

Can you please help as of why this is the case? How do I keep the value in json outside the if(success) statement?

asked Mar 14, 2016 in iOS by khanhtnguyen85 (15 points)

1 Answer

0 votes
 
Best answer

Hi,

I tested the above code snippet in my sample and it is working fine.

I called print(self.json) from another method after the response came and i was able to print the json value.

Could you please provide your sample project where i can reproduce the issue and debug it. May be you can upload your sample project to dropbox and share the link here or on support@shephertz.com.

Thanks.

answered Mar 16, 2016 by rajeev.etc (1,558 points)
selected Mar 17, 2016 by khanhtnguyen85
Hi, thank you so much for your answer. I still can't figure out why it doesn't pass on the value outside the response. I sent my sample project to support but just re-attaching here in case it does not reach the right person.
https://www.dropbox.com/s/knxyttmatr0tck2/MySample.zip?dl=0

Best,
Khanh
Hi Khanh,

I tried your sample and i find out the print statement in the ViewController class of your sample gets executed before the response comes for findAllDocuments API.
Because the the API is an Async call so it does not wait for the response and goes ahead to execute other following statements and hence json value was not set so it was nil.

I just made sure that the response comes and json value is set before calling print on datamanager's json and it started working in your sample.

Let me know if you have any further queries.

Thanks.
Hi Rajeev,

Thank you for your help. That explains a lot. I tried using dispatch_after to delay the print method but json still prints nil. Can I please ask how did you make sure the response come before printing from datamanager?

Thanks,
Khanh
Hi Khanh,
You can implement delegate pattern to notify the ViewController that you got the response. Once response comes you can trigger the delegate method from your DataManager to perform the task need to be done with the response received.
Thanks
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com