0

I am trying to pass the information from the json variable parseJSON["timeStamp"] as! String to strgetTimeStamp, but I tried to add to the return value it keeps bypassing and the return value is always 'nothing'.

When I use print(strgetTimeStamp) after strgetTimeStamp = parseJSON["timeStamp"] as! String, it shows the information.

func checkStamp(AppID:String)-> String{

    //send data to server side
    let myUrl = NSURL(string: "http://chill-lax.website/ReCap/fcm/timeChkRelease.php?apptype=\(AppID)")

    let request = NSMutableURLRequest(URL: myUrl!)
    request.HTTPMethod = "POST"
     var strgetTimeStamp  = "nothing"

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
    {
        data, response, error in

        if error != nil{
            print("error=\(error)")
            return
        }
        do {
            let json = try  NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as? NSDictionary
            // Your code here


            if let parseJSON = json{
                var isTimeStamp :Bool = false
                let resultValue = parseJSON["status"] as! String
                if (resultValue=="Success"){isTimeStamp = true}
                if(resultValue=="Success")
                {
                    strgetTimeStamp = parseJSON["timeStamp"] as! String
                    print(strgetTimeStamp )
                }
            }
            else{
                print("Error - ");
            }
        }
        catch
        {
            print(error)
        }

    }
    task.resume()

    return strgetTimeStamp  ///this returns nothing 
}

*vadian - I am trying to return a value, and the example you showed to me is adding to an NSarray, the json value is only only value. Also this is more obj C ish****

Please can someone tell me where I am going wrong!

hesh
  • 65
  • 4
  • 12
  • The link given by vadian points to Swift answers, nothing "obj C ish"... Also, you were not shown how to add NSArrays but how to get the data out of a closure. Another example: http://stackoverflow.com/a/31264556/2227743 – Eric Aya Aug 30 '16 at 12:18
  • @hesh There is no difference between *I want to return the NSArray* and *I want to return the String* except the type. Basically you cannot return anything. The linked topic shows you how to use completion handlers which work as asynchronous as `NSURLSession` – vadian Aug 30 '16 at 13:05
  • Thanks both for helping! Sorry Vadian i misunderstood the coding .. Eric I followed your example and mange to get the information. But the problem is using let pfd = PostFOrData() pfd.forData { jsonString in print (jsonString) } I get the following value ' {"timeStamp":"2016-08-29 08:08:36"}' ... how can I extract just " "2016-08-29 08:08:36" from jsonString Thanks in advance – hesh Aug 30 '16 at 13:49

0 Answers0