下面的代码行产生了错误,
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
可能不是数组。或至少不是可变数组。如果不研究JSON.object
,我们将很难识别您的问题。快速修复实际上可能是
let response1 : NSMutableArray = NSMutableArray.init(array: (JSON.object as! NSArray).value(forKey: "media_list") as! NSArray)
但是我会尝试进一步挖掘。尝试检查到底发生了什么,并避免使用旧的Objective-C下一步(NS)对象。逐步进行:
let response1: [Any]? = {
guard let mainArray = JSON.object as? [Any] else {
print("Outer object is not an array. Check type of \(JSON.object)")
return nil
}
var mutableVersionOfArray = mainArray // This already creates a mutable copy because we used "var" instead of "let"
guard let mediaList = mutableVersionOfArray.value(forKey: "media_list") as? [Any] else {
print("Inner object is not an array. Check type of \(mutableVersionOfArray.value(forKey: "media_list"))")
return nil
}
return mediaList
}()
但是此代码对我来说毫无意义。查看您的代码,我希望您的JSON对象类似于:
{
"media_list": [{}, {}]
}
在这种情况下,您正在查看词典。尝试以下操作:
let mediaList: [Any]? = {
guard let topDictionary = JSON.object as? [String: Any] else {
print("Outer object is not a dictionary. Check type of \(JSON.object)")
return nil
}
guard let mediaListItem = topDictionary["media_list"] else {
print("There is no media_list in payload")
return nil
}
guard let mediaList = mediaListItem as? [Any] else {
print("mediaList is not an array")
return nil
}
return mediaList
}
我希望您能看到数组和字典之间的区别。数组中有N个有序元素,而字典中有键/值对。因此,要访问键下的值,可以将其称为dictionary[key]
。您的整个代码(如果正确的话)可以是:
let response1 = (JSON.object as? [String: Any])?["media_list"] as? [Any]
但是如果返回nil,则可能很难调试出什么问题。
似乎JSON.object
可能不是数组。或至少不是可变数组。如果不研究JSON.object
,我们将很难识别您的问题。快速修复实际上可能是
let response1 : NSMutableArray = NSMutableArray.init(array: (JSON.object as! NSArray).value(forKey: "media_list") as! NSArray)
但是我会尝试进一步挖掘。尝试检查到底发生了什么,并避免使用旧的Objective-C下一步(NS)对象。逐步进行:
let response1: [Any]? = {
guard let mainArray = JSON.object as? [Any] else {
print("Outer object is not an array. Check type of \(JSON.object)")
return nil
}
var mutableVersionOfArray = mainArray // This already creates a mutable copy because we used "var" instead of "let"
guard let mediaList = mutableVersionOfArray.value(forKey: "media_list") as? [Any] else {
print("Inner object is not an array. Check type of \(mutableVersionOfArray.value(forKey: "media_list"))")
return nil
}
return mediaList
}()
但是此代码对我来说毫无意义。查看您的代码,我希望您的JSON对象类似于:
{
"media_list": [{}, {}]
}
在这种情况下,您正在查看词典。尝试以下操作:
let mediaList: [Any]? = {
guard let topDictionary = JSON.object as? [String: Any] else {
print("Outer object is not a dictionary. Check type of \(JSON.object)")
return nil
}
guard let mediaListItem = topDictionary["media_list"] else {
print("There is no media_list in payload")
return nil
}
guard let mediaList = mediaListItem as? [Any] else {
print("mediaList is not an array")
return nil
}
return mediaList
}
我希望您能看到数组和字典之间的区别。数组中有N个有序元素,而字典中有键/值对。因此,要访问键下的值,可以将其称为dictionary[key]
。您的整个代码(如果正确的话)可以是:
let response1 = (JSON.object as? [String: Any])?["media_list"] as? [Any]
但是如果返回nil,则可能很难调试出什么问题。