ios - Swift: error when trying to communicate with web API -


I'm trying to make a really basic iOS app using Swift - this is my first time to do this Although most of this is actually straight forward, my app has a complete nightmare to speak to my API or to any site in general.

I am using this code, and especially the code that is causing the error:

  NSURLConnection.sendAsynchronousRequest (request, queue: line, ending handler : {(Response: NSURLResponse !, Data: NSData !, Error: NSError) -> Zero (NSString (data: encoding: NSUTF8StringEncoding) in println!)})   

It is the 'NSString (data: data, encoding: NSUTF8StringEncoding)' those errors

as many tutorials as I can find, so I have returned the same problem. I have an 'EXC_BAD_INSTRUCTION' error: EXC_1386_INVOP, sub-code = 0x0 (which gives me a solution:

NSURLErrorDomain - Code: 4294966291

I have not been led ...

I feel that I have not done anything right in my establishment, or something should be running in my priorities, or some of those lines, unlike my code. ..

This is my complete code:

  var URL: NSURL = NS URL (string: "http://stackoverflow.com") Requested on: NSMutableURLRequest = NSMutableURLRequest (url: url) request.HTTPMethod = "post" var bodyData = "key1 = value & amp; Key 2 = Price in & amp; Key3 = value "println (url) println (bodyData) request.HTTPBody = bodyData.dataUsingEncoding (NSUTF8StringEncoding); line Queue: NSOperationQueue = NSOperationQueue () NSURLConnection.sendAsynchronousRequest (request, queue: queue, completionHandler: {(response: NSURLResponse! Data: NSData !, Error: NSError) -> Zero (NSString (Data: Data, Encoding: NSUTF 8th Ring Encoding)) in println)   

Update:

By using Rob's code, I know that an error is printed:

sendAsynchronousRequest error: error domain = NSURLErrorDomain code = -1005 "Network connectors The connection was lost. "UserInfo = 0x7beafcc0 {NSErrorFailingURLStringKey =, _kCFStreamErrorCodeKey = 57, NSErrorFailingURLKey =, NSLocalizedDescription = The network connection was lost., _kCFStreamErrorDomainKey = 1, NSUnderlyingError = 0x7d154ca0" The network connection was data

It looks like data is zero . Error Check the contents of the object, and why you should indicate it is most likely, there is something wrong with request , but until we re-issue the problem By that time, it is difficult to diagnose it.

However, if you want to do more to request the diagnostic information (log error if there is a problem, log status code and Response if the status code was not 200, etc.):

  NSURLConnection.sendAsynchronousRequest (request, queue: queue) {response, data, if error data in == zero {println ("sendAsynchronousRequest error: \ (error)") return} If the feedback responds as HttpResponse =? NSHTTPURLResponse {! If the statusCode = httpResponse.statusCode = statusCode = 200 {println ("sendAsynchronousRequest status code = \ (statusCode); response = \ (response)")}} println (NSString (data: data, encoding: NSUTF8StringEncoding))}   

In your revised question, you as a result have shared NSError which has reported -1005 (which, in the past, As the 4294966291 error code you have previously shown, it is the same as the actual code of -1005 ). This is for error (code as description) for NSURLErrorNetworkConnectionLost (which is kCFURLErrorNetworkConnectionLost ), for which the documentation says "back when a customer or server connection Unfortunately, this error is used for the diverse array of different issues, and as you are unable to reproduce the error by stating, it is difficult to diagnose.


request to disappear from One thing is that content type :

  request.setValue ("application / x-www   

this is by you The problem will not be the cause of the problem, but it is a good practice to always inform the server of the type of request.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -