[英]error==The data couldn’t be read because it isn’t in the correct format. When I use AFNetworking to request
[英]When using the new JSONDecoder protocol in Swift 4 i'm getting the 'the data couldn’t be read because it isn’t in the correct format.' error
Kinda是swift的新功能,尚未使用swift 4解决此错误的解决方案。
使用JSONDecoder函数时,出现一个错误,提示“由于格式不正确,无法读取数据。” 我已经通过验证器运行了JSON,它说那里一切都很好,所以我只是不明白为什么会发生此错误
我的解码代码如下:
import UIKit
class inputscreen: UIViewController {
// the JSONData.php is where the data is being converted into JSON using PhP
final let url = URL(string: "JSONData.php")
override func viewDidLoad() {
super.viewDidLoad()
downloadJson()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func downloadJson() {
guard let downloadURL = url else {return }
URLSession.shared.dataTask(with: downloadURL) {data, urlResponse, error in
guard let data = data, error == nil, urlResponse != nil else {
print("something went wrong")
return
}
// print(data)
print("downloaded")
do {
// this is where the error is happening, everything else is getting through my checks.
let decoder = JSONDecoder()
let variables = try decoder.decode(Variables.self, from:data)
print(variables.variables[0].TblStaffID)
print("success")
} catch {
print(error.localizedDescription)
}
}.resume()
}
}
在JSON中,我已将其转换为UTF8,所以我认为这不是错误的出处
我在另一个看起来像这样的文件中有这个结构:
import Foundation
import UIKit
class Variables: Codable{
let variables: [Variable]
init(variables: [Variable]){
self.variables = variables
}
}
struct Variable: Codable {
let TblStaffID: Int
let User_code: String
let Title: String
let PreName: String
let Surname: String
init(TblStaffID: Int, User_code: String, Title: String, PreName: String, Surname: String){
self.TblStaffID = TblStaffID
self.User_code = User_code
self.Title = Title
self.PreName = PreName
self.Surname = Surname
}
}
在这件事上的任何和所有帮助将不胜感激!
确保将日期格式转换为TimeIntervals或String
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.