Given that other people seem to be using the Xcode 7 branch without problems I suspect that this may be an issue with my code, but on the off-chance that it's not here's my issue.
I'm running Xcode 7 (beta 5) and am working on an iOS 9 app. I added SwiftyJSON to my project the manual way, by dragging it into my source tree. It didn't compile right away, but the errors were pretty simple to fix with Xcode providing fix-it suggestions for all of them.
I am able to retrieve JSON data from a server using the standard NSData/NSJSONSerialization method, but when I attempt to do the same with SwityJSON I get an error. Here's some console output I captured by dropping a breakpoint into the task handler.
(lldb) po JSON(data!)
â–¿ unknown
- rawArray : 0 elements
- rawDictionary : 0 elements
- rawString : ""
- _type : MyApp.Type.Unknown
â–¿ _error : Optional(Error Domain=SwiftyJSONErrorDomain Code=999 "It is a unsupported type" UserInfo={NSLocalizedDescription=It is a unsupported type})
Here's some example code. Note that "requestUrl" below is a string representing a URL which returns valid JSON.
let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: sessionConfig)
let task = session.dataTaskWithURL(requestUrl) { data, response, error in
let json = JSON(data!) // This object doesn't contain anything useful!
do {
let nsJson = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
// At this point I can access the data! eg: nsJson[0]["name"]
} catch {
print("Error")
}
}
task.resume()
Heh. Progress update. I had another think about this and tried plugging the NSJSONSerialization data into SwiftyJSON and it worked! It's not perfect, but for now I'll take it. If anyone has any further insight I'd love to hear it.
let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: sessionConfig)
let task = session.dataTaskWithURL(requestUrl) { data, response, error in
do {
let nsJson = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
let json = JSON(nsJson) // Woohoo!! It works!!
} catch {
print("Error")
}
}
task.resume()
Replace let json = JSON(data!)
by let json = JSON(data:data!)
Thanks very much! Would you mind explaining why that works? What did the original code attempt to do, and why did it fail?
@Subject22 I know this is an old issue, but this is also for everyone who finds this later on.
When you look at the SwiftyJSON initializers you can see that
let json = JSON(data)
would call the initializer public init(_ object: AnyObject)
whereas let json = JSON(data: data)
invokes the dedicated initializer public init(data: NSData)
@ast3150 Thanks!
Most helpful comment
@Subject22 I know this is an old issue, but this is also for everyone who finds this later on.
When you look at the SwiftyJSON initializers you can see that
let json = JSON(data)
would call the initializerpublic init(_ object: AnyObject)
whereas
let json = JSON(data: data)
invokes the dedicated initializerpublic init(data: NSData)