Ios 缺少JSON数组SWIFT
失眠产生了一个代码,它可以工作:Ios 缺少JSON数组SWIFT,ios,json,swift,get,swift4,Ios,Json,Swift,Get,Swift4,失眠产生了一个代码,它可以工作: import Foundation let headers = [ "x-auth-header": "fb2f56fde81f21926fc0b5b74702f71da9f152efa7b5587a308984b71c9acac7fd44da05583ab06d57d29794f59fc475d9f2d132cbc6e29c421f11330a30a613", "content-type": "application/x-www-form-urlen
import Foundation
let headers = [
"x-auth-header": "fb2f56fde81f21926fc0b5b74702f71da9f152efa7b5587a308984b71c9acac7fd44da05583ab06d57d29794f59fc475d9f2d132cbc6e29c421f11330a30a613",
"content-type": "application/x-www-form-urlencoded"
]
let postData = NSData(data: "".data(using: String.Encoding.utf8)!)
let request = NSMutableURLRequest(url: NSURL(string: "http://rekrutacja.backendzs.pl/note/")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? HTTPURLResponse
print(httpResponse)
}
})
dataTask.resume()
它可以工作,并且必须返回项目数组:
[
{
"note_id": 14,
"title": "last one",
"content": "maybe",
"version": 16,
"created": 1530825998,
"modified": 1530825998
},
{
"note_id": 13,
"title": "again",
"content": "again",
"version": 15,
"created": 1530825532,
"modified": 1530825532
},
{
"note_id": 12,
"title": "zzz",
"content": "zzzz",
"version": 14,
"created": 1530825484,
"modified": 1530825484
},
{
"note_id": 11,
"title": "Nowy tytuł",
"content": "4",
"version": 13,
"created": 1530825331,
"modified": 1530825331
},
{
"note_id": 10,
"title": "test5",
"content": "test",
"version": 12,
"created": 1530825252,
"modified": 1530825252
},
{
"note_id": 9,
"title": "title7",
"content": "1111",
"version": 11,
"created": 1530825225,
"modified": 1530825225
},
{
"note_id": 7,
"title": "test",
"content": "content",
"version": 9,
"created": 1530817192,
"modified": 1530817192
},
{
"note_id": 6,
"title": "Tytuł",
"content": "Treść",
"version": 8,
"created": 1530550847,
"modified": 1530550847
},
{
"note_id": 5,
"title": "Tytuł",
"content": "Treść",
"version": 7,
"created": 1530547099,
"modified": 1530547099
},
{
"note_id": 3,
"title": "title",
"content": "content",
"version": 3,
"created": 1530469102,
"modified": 1530469102
},
{
"note_id": 2,
"title": "title",
"content": "content",
"version": 2,
"created": 1530468369,
"modified": 1530468369
}
]
但是,它不提供此列表以及来自服务器的响应。仅显示常规数据。我已经测试并检查了与Alamofire、swiftyJSON、Swift.get请求相关的所有文章,但都不起作用
考虑到这一信息:
如果在
httpResponse
中打印内容,则需要使用可解码返回的数据
struct Item : Decodable {
let title:String,content:String,version:Int,created:Int32,modified:Int32,noteId:Int
private enum CodingKeys : String, CodingKey {
case title,content,version,created,modified,noteId = "note_id"
}
}
//
内部响应完成
do {
let items = try JSONDecoder().decode([Item].self, from: data)
print(items)
}
catch {
print(error)
}
类型“Item”不符合类之前结构声明的协议“Decodable”,但它有一些错误。\ux。已经对此进行了搜索,谢谢)也许它应该只是带有参数“Decodable”的“struct”或“extension”?由于苹果的文档,它现在可以工作了,没有逗号。伙计!让上帝保佑你!))你已经在5分钟内解决了我几天来一直试图解决的问题。衷心感谢你!快乐编码=D