I'm trying to unwrap these Int values that I receive from the server, but everything I've tried it just doesn't work. It keeps printing:
"Optional(int value)".
var playerId, roomId : Int!
func makeGetRequest(path: String){
let urlPath: String = "http://myServer.ddns.net:7878/\(path)"
let url: NSURL = NSURL(string: urlPath)!
var request1: URLRequest = URLRequest(url: url as URL)
request1.httpMethod = "GET"
let queue:OperationQueue = OperationQueue()
NSURLConnection.sendAsynchronousRequest(request1 as URLRequest, queue: queue, completionHandler:{ (response: URLResponse?, data: Data?, error: Error?) -> Void in
do {
let jsonData = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary
print("ASynchronous\(jsonData)")
self.playerId = jsonData.value(forKey: "playerId") as! Int
self.roomId = jsonData.value(forKey: "roomId") as! Int
print("\(self.roomId) \(self.playerId)")
} catch let error as NSError {
print(error.localizedDescription)
}
})
}
That's how optionals print. If you don't want that, then you have to unwrap them before printing them. Keep in mind that the comments on your question are valid, and the "!" will almost certainly crash you at some point. But just for brevity, you can do this:
print("\(self.roomId!) \(self.playerId!)")