Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 类型';(字符串,任意对象)和#x27;在swift中没有下标成员_Ios_Swift - Fatal编程技术网

Ios 类型';(字符串,任意对象)和#x27;在swift中没有下标成员

Ios 类型';(字符串,任意对象)和#x27;在swift中没有下标成员,ios,swift,Ios,Swift,我用这条线来获取数据 let productDict = arrProductCart[sender.tag] as! [String: AnyObject] 我想从字典中过滤数据,所以我使用这段代码 let filteredSubitems = productDict.filter{ $0["groupid"] as!String != "1" } 它给了我一个错误Type'(String,AnyObject)'没有下标成员 是否需要将[String:AnyOb

我用这条线来获取数据

 let productDict  = arrProductCart[sender.tag] as! [String: AnyObject]
我想从字典中过滤数据,所以我使用这段代码

let filteredSubitems = productDict.filter{
        $0["groupid"] as!String != "1"
    }
它给了我一个错误Type'(String,AnyObject)'没有下标成员

是否需要将[String:AnyObject]转换为[String:String]?
做什么。

您很可能想过滤
arrProductCard
数组,而不是
productDict
,这毫无意义。试试这个:

let filteredProducts = arrProductCard.filter{
    guard let groupId = $0["groupid"] as? String else { return false }
    return groupId != "1"
}
您应该尽可能避免强制展开。请注意,如果字典中没有
groupid
值或它不是字符串,则筛选器闭包中的代码将崩溃

编辑:

如果出于某种原因使用了
NSMutableArray
,则可以使用谓词对其进行筛选:

let mutableArray = NSMutableArray(array: [["groupid": "1"], ["groupid": "2"]])    
let groupIdPredicate = NSPredicate(format: "groupid != %@", "1")
mutableArray.filter(using: groupIdPredicate)

但是,我强烈建议您使用Swift native collections。

您很可能希望筛选
arrProductCard
数组,而不是
productDict
,这是没有意义的。试试这个:

let filteredProducts = arrProductCard.filter{
    guard let groupId = $0["groupid"] as? String else { return false }
    return groupId != "1"
}
您应该尽可能避免强制展开。请注意,如果字典中没有
groupid
值或它不是字符串,则筛选器闭包中的代码将崩溃

编辑:

如果出于某种原因使用了
NSMutableArray
,则可以使用谓词对其进行筛选:

let mutableArray = NSMutableArray(array: [["groupid": "1"], ["groupid": "2"]])    
let groupIdPredicate = NSPredicate(format: "groupid != %@", "1")
mutableArray.filter(using: groupIdPredicate)

但是,我强烈建议使用Swift本机集合。

[String:AnyObject]
这意味着字典。。。notarray和dictionary没有下标memberFilter、Map、Reduce用于array、Set(通常是可以枚举的数据结构)。因此出现了错误。@krutarthatel在
groupId
值不是字符串时要小心。不要使用强制展开
[String:AnyObject]
这意味着字典。。。notarray和dictionary没有下标memberFilter、Map、Reduce用于array、Set(通常是可以枚举的数据结构)。因此出现了错误。@krutarthatel在
groupId
值不是字符串时要小心。不要为了一个问题的好解释而强行展开感激之情。数据已筛选。但在arrProductCard中,数据仍然存在。是否可以删除它/只需在同一变量中设置结果:
arrProductCard=arrProductCard.filter{…}
我使用NSMUTUABALARRAY,因此获取错误无法将类型“[Element]”的值分配给类型“NSMUTABLEARY”@KrutarthPatel我不知道为什么将“groupid”键更改为“NewGroupID”感谢您对一个问题的精彩解释。数据已被过滤。但在arrProductCard中,数据仍然存在。是否可以将其删除/仅在同一变量中设置结果:
arrProductCard=arrProductCard.filter{…}
我使用NSMUTUABALEARRAY,因此获取错误无法分配“[Element]类型的值'键入'NSMutableArray'@KrutarthPatel我不知道为什么您将“groupid”键更改为“NewGroupID”