Reputation: 6752
I am getting this error:
Cannot invoke initialiser for type 'JSON' with an argument list of type (data: NSUrlConnection?)
When I try to execute this code:
var url = NSURL(string: "http://****")
var request = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: Double.infinity);
if IJReachability.isConnectedToNetwork(){
request = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: Double.infinity);
}
var response: NSURLResponse?
let data = NSURLConnection?
do {
data = try NSURLConnection.sendSynchronousRequest(request, returningResponse: &response)
} catch (let e) {
print(e)
}
if data != nil {
var dataArray = JSON(data: data)
let dutch_sentence = dataArray[id]["dutch_sentence"]
let polish_sentence = dataArray[id]["polish_sentence"]
let navigationTitle = dutch_sentence.string!.uppercaseString
self.title = navigationTitle
//Populate labels
dutchSentenceLabel.text = dutch_sentence.string!
polishSentenceLabel.text = polish_sentence.string!
}
I am new to Swift and I am trying to fix the errors out of my code since it is updated but I am having a hard time on it since I am not experienced with the language.. can someone please help me out? I wouldn't be surprised if I messed up the whole code already actually..
I also tried this:
let url = NSURL(string: "http://****")
var request = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: Double.infinity);
if IJReachability.isConnectedToNetwork(){
request = NSMutableURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: Double.infinity);
}
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
var dataArray = JSON(data: data!)
let dutch_sentence = dataArray[self.id]["dutch_sentence"]
let polish_sentence = dataArray[self.id]["polish_sentence"]
let navigationTitle = dutch_sentence.string!.uppercaseString
self.title = navigationTitle
//Populate labels
self.dutchSentenceLabel.text = dutch_sentence.string!
self.polishSentenceLabel.text = polish_sentence.string!
}
task.resume()
It has no errors but I don't know if it is the correct way and if it will work. I have to replace it on a lot of places so I want to be sure about if it works before I do that.
Upvotes: 0
Views: 840
Reputation: 125
I believe you were intending to use JSONObjectWithData:options:error
Unless you were doing this using SwiftyJSON. If so, here is an excellent article detailing how to install and use it.
Upvotes: 1