I am trying to write a method that scan the barcode then using http rest call to get some JSON data from a server. Alamofire doesn't work now and I tried many different ways.
Here is what I got now:
let getEndpoint: String = "45.55.63.218:8080/food?foodidentifier=\(code)"
let requestURL: NSURL = NSURL(string: getEndpoint)!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(urlRequest) {
(data, response, error) -> Void in
let httpResponse = response as! NSHTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
print("Everyone is fine, file downloaded successfully.")
do{
//let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)
}catch {
print("Error with Json: \(error)")
}
}
}
task.resume()
I get an error message:
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb) on line: let httpResponse = response as! NSHTTPURLResponse