ios - Type '(String, AnyObject)' has no subscript members in swift -
i using line data
let productdict = arrproductcart[sender.tag] as! [string: anyobject]
and want filter data dictionary using code
let filteredsubitems = productdict.filter{ $0["groupid"] as!string != "1" }
it giving me error type '(string, anyobject)' has no subscript members
do need convert [string: anyobject] [string: string]? do.
most want filter arrproductcard
array instead of productdict
, doesn't make sense. try this:
let filteredproducts = arrproductcard.filter{ guard let groupid = $0["groupid"] as? string else { return false } return groupid != "1" }
you should avoid forced unwrapping whenever can. note code inside filter closure crash if there no groupid
value in dictionary or if not string.
edit:
if you're using nsmutablearray
reason, can filter predicate:
let mutablearray = nsmutablearray(array: [["groupid": "1"], ["groupid": "2"]]) let groupidpredicate = nspredicate(format: "groupid != %@", "1") mutablearray.filter(using: groupidpredicate)
however, recommend use swift native collections.
Comments
Post a Comment