我有一個快速的代碼我的問題。我通過httpMethod POST向web服務器發出請求。這個請求沒問題。我在數據值中獲得響應和數據。數據看起來像JSONHTTP請求GET JSON並讀取數據
{"pushValues": {"devicePushGlobal":"1","devicePushNewProducts":"1","devicePushNewOffer":"1"}}
然後我會加載這個響應數據來設置基於響應數據的按鈕。但我沒有寫這個代碼。有人能幫助我嗎? :)
錯誤代碼 Cannot invoke 'jsonObject' with an argument list of type '(with: NSString)'
//我測試了其他的選擇,但我總是失敗:-(
我評論了錯誤代碼....
let url = "https://URL.php"
let request = NSMutableURLRequest(url: NSURL(string: url)! as URL)
let bodyData = "token=" + (dts)
request.httpMethod = "POST"
request.httpBody = bodyData.data(using: String.Encoding.utf8);
NSURLConnection.sendAsynchronousRequest(request as URLRequest, queue: OperationQueue.main) {
(response, data, error) in
// here i get the result of
// {"pushValues": {"devicePushGlobal":"1","devicePushNewProducts":"1","devicePushNewOffer":"1"}}
var str = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
var names = [String]()
// here i will get each value of pushValues to add to the array names
do {
if let data = str,
// ... and here is the error code by xcode ::: ==> Cannot invoke 'jsonObject' with an argument list of type '(with: NSString)'
// i tested with other options but i always fail :-(
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any],
let blogs = json["pushValues"] as? [[String: Any]] {
for blog in blogs {
if let name = blog["devicePushGlobal"] as? String {
print(name)
names.append(name)
}
}
}
} catch {
print("Error deserializing JSON: \(error)")
}
// names array is empty
print(names)
}
謝謝你您的幫助
刪除'如果讓數據= str'。 – Larme