【发布时间】:2019-11-22 09:22:00
【问题描述】:
我遇到了一些我认为很简单的问题,但不知何故我想不出任何方法来解决它。它只是将一个 NSArray 转换为一个数组,我认为我在这里用这段代码做得很好:
func getTrainingSubCategories(completion: @escaping(Result<[TrainingSubCategory], GetError>) -> Void){
let dataTask = URLSession.shared.dataTask(with: resourceURL){data, _, _ in
guard let jsonData = data else {
completion(.failure(.trainingCategoriesNoDataAvailable))
return
}
do{
let parsedData = try JSONSerialization.jsonObject(with: jsonData) as! [String: Any]
for (key, value) in parsedData {
if key == "categories" {
if let categoriesDictionary: [[String: Any]] = value as? [[String: Any]]{
for categoryDictionary in categoriesDictionary{
for (key, value) in categoryDictionary{
if key == "sub_categories" {
print("1st Type => \(type(of: value)) Value => \(value)")
print("2nd Type => \(type(of: value as? Array<TrainingSubCategory>)) Value => \(value as? Array<TrainingSubCategory>)")
// guard let subCategoryValue = value as? Array<TrainingSubCategory> else{
// fatalError("Error in casting value to array of trainingsubcategory")
// }
}
}
}
}
}
}
}catch {
completion(.failure(.trainingSubCategoriesCannotProcessData))
}
}
dataTask.resume()
}
但控制台会记录其他情况:
1st Value => (
{
"category_id" = 1;
"company_id" = 50;
"created_at" = "2019-11-07 00:58:37";
"deleted_at" = "<null>";
id = 1;
name = Advertisement;
"updated_at" = "2019-11-07 00:58:37";
},
{
"category_id" = 1;
"company_id" = 50;
"created_at" = "<null>";
"deleted_at" = "<null>";
id = 3;
name = Sales;
"updated_at" = "2019-11-10 23:28:30";
}
)
2nd Value => nil
1st Value => (
{
"category_id" = 2;
"company_id" = 50;
"created_at" = "2019-11-07 19:58:07";
"deleted_at" = "<null>";
id = 6;
name = Inventory;
"updated_at" = "2019-11-07 19:58:07";
}
)
2nd Value => nil
1st Value => (
{
"category_id" = 3;
"company_id" = 50;
"created_at" = "2019-11-10 21:24:22";
"deleted_at" = "<null>";
id = 9;
name = "Human Resource";
"updated_at" = "2019-11-10 21:24:22";
}
)
2nd Value => nil
我在这里遗漏了什么吗?我对 Swift 很陌生,所以我自己实际上并没有太多猜测。
【问题讨论】:
-
您的 TrainingSubCategory 类/结构是否符合 Codable 协议?
-
您不能将值转换为特定类型的数组,您需要手动将字典映射到您的自定义类型 TrainingSubCategory。正如前面的评论所暗示的那样,也许最简单的方法是向我们
Codable解码您的 json -
这不是
TrainingSubCatgory的数组,而是字典的数组。它不被解析为 TrainingSubCatgory,它是字典。 -
@KeshuRai,是的。 :)
-
@JoakimDanielson,我已经让它可编码了。我按照你说的做了,手动将其映射到 TrainingSubCategory。它工作得很好,谢谢!我会显示代码
标签: arrays json swift dictionary nsarray