在您的文章中,您缺少實際獲取數據的部分。
你的代碼應該看起來像這樣從文本文件中獲取值。
var lastID: String?
let myURL = NSURL(string:"https://api.thingspeak.com/channels/1417/last_entry_id.txt");
let request = NSMutableURLRequest(url:myURL! as URL);
//request.httpMethod = "GET" // This line is not need
// Excute HTTP Request
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
// Check for error
if error != nil
{
print("error=\(error)")
return
}
// Print out response string
let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
print("responseString = \(responseString!)")
lastID = "\(responseString!)" // Sets some variable or text field. Not that its unwrapped because its an optional.
}
task.resume()
你有什麼試過的?你想要檢索什麼類型的數據? XML還是JSON?顯示您嘗試過的代碼或有問題的代碼會讓您獲得更好的迴應。 – MwcsMac
[如何在Swift中創建HTTP請求?]可能重複(http://stackoverflow.com/questions/24016142/how-to-make-an-http-request-in-swift) –
@MwcsMac我已更新我的問題在上面。 –