gpt4 book ai didi

JSON 下载但没有添加到 UITable,但没有错误?

转载 作者:搜寻专家 更新时间:2023-11-01 07:18:19 26 4
gpt4 key购买 nike

我有一个 API 可以加载到我的应用中:

https://wger.de/api/v2/exercise/?language=2

API 调用没有错误,运行获取数据函数的 TableView 也没有错误。到现在为止,它以前一直运行良好。我没有更改任何代码。

我以为服务器会因通话而停机,但正如您所见,它们并非如此。

我正在运行打印“请求成功”,它在运行时显示在调试中,因此它获取数据,但是打印(self.exercises)返回一个空数组因此没有表数据......有什么想法吗?这是 API 调用

open class ApiService: NSObject {

open func getData(completionHandler: @escaping (NSDictionary?, NSError?) -> Void) -> Self {

//loads api filtering by english only
let requestUrl = "https://wger.de/api/v2/exercise/?format=json&language=2"

Alamofire.request(requestUrl, method: .get, encoding: URLEncoding.default)
.responseJSON { response in
switch response.result {
case .success( let data):
print("Request was sucessful")
completionHandler(data as? NSDictionary, nil)

case .failure(let error):
print("Request failed with error: \(error)")
completionHandler(nil, error as NSError?)
}
}
return self
}

表函数

        func getApiData() {

let _ = apiService.getData() {
(data, error) in
if let data = data {
if let results = data["results"] as? [[String:Any]] {
for result in results {
if let exercise = Exercise(dictionary: result) {
self.exercises.append(exercise)
}
}
self.exercisesTableView.reloadData()
print(self.exercises)
}
}
}
}

我也在使用序列化模型,如果这会干扰?

final public class Exercise {
var id: Int
var descrip: String
var name: String
var language: [Int]
var muscles: [Int]
var musclesSecondary: [Int]
var equipment: [Int]

public init?(dictionary: [String: Any]) {

guard
let id = dictionary["id"] as? Int,
let descrip = dictionary["description"] as? String,
let name = dictionary["name"] as? String,
let language = dictionary["language"] as? [Int],
let muscles = dictionary["muscles"] as? [Int],
let musclesSecondary = dictionary["muscles_secondary"] as? [Int],
let equipment = dictionary["equipment"] as? [Int]
else { return nil }

self.id = id
self.descrip = descrip
self.name = name
self.language = language
self.muscles = muscles
self.musclesSecondary = musclesSecondary
self.equipment = equipment
}

最佳答案

对不起,

I havent changed any code

错了。

您添加了属性 musclesSecondarylanguage 并且确实存在错误:language is single Int rather而不是一个数组。

var language: Int
...
let language = dictionary["language"] as? Int,

关于JSON 下载但没有添加到 UITable,但没有错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40609194/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com