I am trying to build a really basic iOS app using Swift - this is my first time doing so. Whilst the majority of it has been really straight forward. I have had a total nightmare getting the app to talk to my API, or any site in general!
This is the code i'm using, and specifically the code which is erroring:
NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
})
it's the 'NSString(data: data, encoding: NSUTF8StringEncoding)' that errors
I've gone through as many tutorials as I can find, and they all return me the same problem. I get an error saying:
NSURLErrorDomain - code: 4294966291
I also get a 'EXC_BAD_INSTRUCTION' error with: EXC_1386_INVOP, sub code = 0x0 (which hasn't led me to a solution...
Which makes me think that I haven't done something right in my set up, or something should be turned on in my preferences, or something along those lines as opposed to my code...
this is my whole code:
var URL: NSURL = NSURL(string: "http://stackoverflow.com")
var request:NSMutableURLRequest = NSMutableURLRequest(URL:URL)
request.HTTPMethod = "POST"
var bodyData = "key1=value&key2=value&key3=value"
println(URL)
println(bodyData)
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);
let queue:NSOperationQueue = NSOperationQueue()
NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
})
Update:
Using Rob's code, I know get an error printed:
sendAsynchronousRequest error: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo=0x7beafcc0 {NSErrorFailingURLStringKey=http://stackoverflow.com, _kCFStreamErrorCodeKey=57, NSErrorFailingURLKey=http://stackoverflow.com, NSLocalizedDescription=The network connection was lost., _kCFStreamErrorDomainKey=1, NSUnderlyingError=0x7d154ca0 "The network connection was lost."}