In Xcode 6.3 beta running Swift 1.2 I have seemingly successfully retrieved a JSON object from an API, but the debugger is claiming that the object has no properties. The API is running on localhost and is called like this:
NSURLConnection.sendAsynchronousRequest( request, queue: queue,
completionHandler: completionHandler )
completionHandler
is a closure with a breakpoint in it:
let completionHandler =
{
( response: NSURLResponse!, data: NSData!, error: NSError! ) -> Void in
// BREAKPOINT HERE
}
The debugger shows that in Xcode 6.3 beta with Swift 1.2 data: NSData! is an ObjectiveC.NSObject of 190 bytes but with no properties, which seems flatly contradictory. I simply want the JSON data that I hope is hidden somewhere in those bytes. I do not know whether the debugger, or code that needs updating for Swift 1.2, or my understanding of things, or something else is to blame. I simply want the JSON.
Post-script
The JSON data was there all along, as the byte count suggested, and getting it was very simple (using SwiftyJSON):
let json = JSON( data: data )
Quite what has happened to the properties in the Xcode 6.3 beta debugger remains a mystery though. I'm not adding this as an answer to my own question, because whilst it works it might not necessarily be the answer. I am too new to Swift to know what the actual answer might be, or even if there is one.
NSData is an encapsulation of the raw data bytes, before it's translated into a string or parsed as JSON.
If you want to see what this data contains, then you can use NSJSONSerialization (let jsonDictionary = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil)
)
and then read the dictionary like any other: let value = jsonDictionary["key"]
I hope that's helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With