【发布时间】:2020-01-27 06:12:22
【问题描述】:
下面的代码行产生了错误,
let response1 : NSMutableArray = NSMutableArray.init(array: (JSON.object as! NSMutableArray).value(forKey: "media_list") as! NSArray)
正如错误所说,我理解它是一个强制转换异常,但我无法修改代码以使其工作。我对 Swift 有点陌生,所以任何帮助都将不胜感激。 下面是我的 JSON.object
所以,我检查了一下,这是我的 JSON.object
[["offset": 30119146, "file_size": 30119146, "filename": video_220120201129271580.mp4, "mediaPath": file:///Users/evuser/Library/Developer/CoreSimulator/Devices/B9B0232F-237D-4413-BB81-BD5FAC727305/data/Containers/Data/Application/401D5D91-4500-434A-98FE-BD416135A1C7/Documents/video_220120201129271580.mp4, "status": completed, "group_id": fKQ2Xd9bE0cXchsw, "createdDate": 2020/01/22 13:59:47, "_id": 5e27e4d3138c8801cd3c26ca, "user_id": 21, "mime_type": video/mp4, "dest_path": /video_220120201129271580.mp4, "resource_id": 3a743d84-eafe-41e5-9f4c-dece67598c32],
["offset": 6435018, "file_size": 6435018, "filename": video_220120201127525480.mp4, "mediaPath": file:///Users/evuser/Library/Developer/CoreSimulator/Devices/B9B0232F-237D-4413-BB81-BD5FAC727305/data/Containers/Data/Application/401D5D91-4500-434A-98FE-BD416135A1C7/Documents/video_220120201127525480.mp4, "status": completed, "group_id": ffoHuGL0Z17vOqY9, "createdDate": 2020/01/22 13:58:10, "_id": 5e27e472138c8801cd3c26c9, "user_id": 21, "mime_type": video/mp4, "dest_path": /video_220120201127525480.mp4, "resource_id": 50e34fd5-b488-4861-aedd-03ea1ed0d91c]]
【问题讨论】:
-
JSON.object的类型是什么? -
首先,很少有理由在 Swift 中使用
NSArray或NSMutable数组。您崩溃的原因是JSON.object似乎是NSArray而不是NSMutableArray。你的整行没有多大意义 - 你不能在NSArray上使用value(forKey:)- 这是在NSDictionary上使用的 - 你也不应该在 Swift 中使用它。 JSON 的最佳方法是使用Codable并创建适当的结构
标签: ios swift iphone nsmutablearray nsarray