我有一些代碼,我用了一段時間後,完美的工作。現在,我想在我的新項目,這是SWIFT 3使用它,雖然我有固定的大多數錯誤,我仍然有兩人離開如下:iOS Swift 3 POST問題
下面的代碼行:var _: NSData? = NSURLConnection.sendSynchronousRequest(request as URLRequest, returning:&response) as NSData
產生以下錯誤:Call can throw, but it is not marked with 'try' and the error is not handled
以下代碼行:NSLog("Response code: %ld", res?.statusCode ?? <#default value#>);
產生以下錯誤:Editor placeholder in source file
。
任何幫助表示讚賞。
下面是完整的代碼,可以幫助解決這個問題:
func webViewDidFinishLoad(_ webView: UIWebView) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
let post:NSString = "userid=349&devicetoken=walan"
NSLog("PostData: %@",post);
let url:NSURL = NSURL(string: "https://str8red.com/updateAPN")!
let postData:NSData = post.data(using: String.Encoding.ascii.rawValue)! as NSData
let postLength:NSString = String(postData.length) as NSString
let request:NSMutableURLRequest = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
request.httpBody = postData as Data
request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
var _: NSError?
var response: URLResponse?
var _: NSData? = NSURLConnection.sendSynchronousRequest(request as URLRequest, returning:&response) as NSData
let res = response as! HTTPURLResponse!;
NSLog("Response code: %ld", res?.statusCode ?? <#default value#>);
}
如果您滿意,請接受並回復它。如果您有其他問題,請發表評論。 – Wismin