2017-05-26 92 views
0

我有一些代碼,我用了一段時間後,完美的工作。現在,我想在我的新項目,這是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#>); 

} 

回答

1

您可以添加關鍵字try和環繞聲和do {}趕上{}聲明如下:

do { 

try NSURLConnection.sendSynchronousRequest(request as URLRequest, returning:&response) as NSData 
} catch { 
    print(error) 
} 

你將仍然有一個警告,該功能已被棄用,因爲iOS 9。

我會看看嘗試看看整個功能如何可以重寫在iOS 10,Swift 3.1語法,但有一個da說它可能會破壞,並且與其他遺留代碼看起來會非常不同。敬請期待,我會再次更新這個答案。

至於「佔位符錯誤」你可以把像「500」的一些值,例如默認爲「內部服務器錯誤」

NSLog("Response code: %ld", res?.statusCode ?? 500); 

更新:這裏是更新到斯威夫特3的功能, iOS 10.

我只根據函數的原意進行更新。只需語法/ API更新,不刪除/添加任何功能。

func webViewDidFinishLoad(_ webView: UIWebView) { 
    UIApplication.shared.isNetworkActivityIndicatorVisible = false 

    let post:String = "userid=349&devicetoken=walan" 

    NSLog("PostData: %@",post); 

    let url:URL = URL(string: "https://str8red.com/updateAPN")! 

    let postData:Data = post.data(using: String.Encoding(rawValue: String.Encoding.ascii.rawValue))! 

    let postLength: String = String(postData.count) 

    var request:URLRequest = URLRequest(url: url as URL) 

    request.httpMethod = "POST" 
    request.httpBody = postData as Data 
    request.setValue(postLength, forHTTPHeaderField: "Content-Length") 
    request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") 
    request.setValue("application/json", forHTTPHeaderField: "Accept") 

    var _: NSError? 
    var response: URLResponse? 

    URLSession.shared.dataTask(with: request) { (data, res, error) in 

     guard error == nil else { 
      print(error!) 
      return 
     } 

     response = res 
    } 

    let res = response as! HTTPURLResponse!; 

    NSLog("Response code: %ld", res?.statusCode ?? 500); 
} 
+0

如果您滿意,請接受並回復它。如果您有其他問題,請發表評論。 – Wismin