无法使用 String 类型的索引下标 NSDictionary 类型的值。从 Swift 2.3 -> 3.2 转换时
Cannot subscript a value of type NSDictionary with an index of type String. While converting from Swift 2.3 -> 3.2
我需要帮助。从 Swift 2.3 -> 3.2 转换时,我收到以下错误。我无法解决此错误。
以下是我的编码工作,我遇到了一些问题。
错误:
Cannot subscript a value of type NSDictionary with an index of type String
在这一行:if let tempValue:AnyObject = tempDict["value"] {
if (productToReturn.planoRetailPackSize == nil || productToReturn.planoRetailPackSize == "0.0") {
if let dataToProcess:NSDictionary = dict["data"] as? NSDictionary {
if let productDataRecord:NSDictionary = dataToProcess["productDataRecord"] as? NSDictionary{
if let module:NSArray = productDataRecord["module"] as? NSArray{
for (_,value) in module.enumerated(){
if let parentDic:NSDictionary = value as? NSDictionary{
if let cpmChild:NSDictionary = parentDic["cem:canadaExtensionModule"] as? NSDictionary {
if let tempDict:NSDictionary = cpmChild["retailPackSize"] as? NSDictionary {
if let tempValue:AnyObject = tempDict["value"] { //Error is Here
let myValue: String = String(describing: tempValue)
productToReturn.planoRetailPackSize = myValue
}
}//item
}
}
}
}
}
}
}
请帮帮我。我对 iOS 很陌生。无法理解此类错误。
最佳答案是使用本机 Swift 类型。另一种方法是将下标转换为 NSString
.
...
if let tempValue:AnyObject = tempDict["value" as NSString] {
...
使用原生类型
if let dataToProcess = dict["data"] as? [String:Any],
let productDataRecord = dataToProcess["productDataRecord"] as? [String:Any],
let module = productDataRecord["module"] as? [[String:Any]] {
for value in module {
if let cpmChild = value["cem:canadaExtensionModule"] as? [String:Any],
let tempDict = cpmChild["retailPackSize"] as? [String:Any],
let myValue = tempDict["value"] as? String {
productToReturn.planoRetailPackSize = myValue
}
}
}
注意:在 for
循环中,myValue
将在每次迭代中覆盖 planoRetailPackSize
。这很可能不是故意的。
我需要帮助。从 Swift 2.3 -> 3.2 转换时,我收到以下错误。我无法解决此错误。
以下是我的编码工作,我遇到了一些问题。
错误:
Cannot subscript a value of type NSDictionary with an index of type String
在这一行:if let tempValue:AnyObject = tempDict["value"] {
if (productToReturn.planoRetailPackSize == nil || productToReturn.planoRetailPackSize == "0.0") {
if let dataToProcess:NSDictionary = dict["data"] as? NSDictionary {
if let productDataRecord:NSDictionary = dataToProcess["productDataRecord"] as? NSDictionary{
if let module:NSArray = productDataRecord["module"] as? NSArray{
for (_,value) in module.enumerated(){
if let parentDic:NSDictionary = value as? NSDictionary{
if let cpmChild:NSDictionary = parentDic["cem:canadaExtensionModule"] as? NSDictionary {
if let tempDict:NSDictionary = cpmChild["retailPackSize"] as? NSDictionary {
if let tempValue:AnyObject = tempDict["value"] { //Error is Here
let myValue: String = String(describing: tempValue)
productToReturn.planoRetailPackSize = myValue
}
}//item
}
}
}
}
}
}
}
请帮帮我。我对 iOS 很陌生。无法理解此类错误。
最佳答案是使用本机 Swift 类型。另一种方法是将下标转换为 NSString
.
...
if let tempValue:AnyObject = tempDict["value" as NSString] {
...
使用原生类型
if let dataToProcess = dict["data"] as? [String:Any],
let productDataRecord = dataToProcess["productDataRecord"] as? [String:Any],
let module = productDataRecord["module"] as? [[String:Any]] {
for value in module {
if let cpmChild = value["cem:canadaExtensionModule"] as? [String:Any],
let tempDict = cpmChild["retailPackSize"] as? [String:Any],
let myValue = tempDict["value"] as? String {
productToReturn.planoRetailPackSize = myValue
}
}
}
注意:在 for
循环中,myValue
将在每次迭代中覆盖 planoRetailPackSize
。这很可能不是故意的。