0
我對swift很陌生,我試圖在3列中顯示從服務器接收到的JSON數據,但是,我希望文本排列並可能刪除括號圍繞每一段文字。我在下面附上了一張圖片,以及我的代碼。在Swift中很好地顯示JSON數據3
let u = UserDefaults.standard.value(forKey: "userIP")!
var request = URLRequest(url: URL(string: "http://\(u):3000/logs")!)
request.httpMethod = "POST"
let postString = "LockID=\(lockid)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
DispatchQueue.main.async() {
self.displayMyAlertMessage("response = \(response)")
}
}
let responseString = String(data: data, encoding: .utf8)
// print("responseString = \(responseString)")
if let data = responseString?.data(using: String.Encoding.utf8) {
let resString = JSON(data: data)
if resString["success"].stringValue == "true"
{
// save the data to be manipulated and stored in table somehow
let returned_name = resString["message"].arrayValue.map({$0["name"].stringValue})
let returned_time = resString["message"].arrayValue.map({$0["lockTime"].stringValue})
let returned_type = resString["message"].arrayValue.map({$0["type"].stringValue})
DispatchQueue.main.async() {
self.updatename!.text = "\(returned_name.description)"
self.updateLock!.text = " \(returned_type.description)"
self.updateText!.text = " \(returned_time.description)"
}
}
else if resString["success"].stringValue == "false"
{
DispatchQueue.main.async() {
self.displayMyAlertMessage(resString["message"].stringValue)
}
}
林maniplulating與SwiftyJSON JSON數據。關於我如何刪除[「」]的任何想法?
乾杯
感謝您的幫助!出於好奇,是否有理由在蛇殼上使用駱駝案件? – CS456
這是Objective-C和Swift中的命名約定 – vadian