下面提到的是我的优惠券数组,我想删除包含' x'代码
(
{
"coupon_code" = FLAT20PERCENT;
}
{
“coupon_code” = FLAT5PERCENT;
}
{
“coupon_code” = FLAT50;
}
)
答案 0 :(得分:1)
首先,为什么不尝试在他们的NS
对手上使用Swift的Array
和Dictionary
结构?这将使您的工作更轻松,并使您的代码更简洁:
Objective-C方式:
let array = NSMutableArray(array: [
NSMutableDictionary(dictionary: ["coupon_code": "FLAT50PERCENT"]),
NSMutableDictionary(dictionary: ["coupon_code": "FLAT5PERCENT"]),
NSMutableDictionary(dictionary: ["coupon_code": "FLAT50"])
])
迅捷的方式:
(另外,与上面不同,你不会失去类型。)
var array = [
["coupon_code": "FLAT50PERCENT"],
["coupon_code": "FLAT5PERCENT"],
["coupon_code": "FLAT50"]
]
无论如何,如果你坚持使用Objective-C的集合类,这是一种方法:
let searchString = "PERCENT"
let predicate = NSPredicate(format: "coupon_code contains[cd] %@", searchString)
// change it to "coupon_code == @" for checking equality.
let indexes = array.indexesOfObjects(options: []) { (dictionary, index, stop) -> Bool in
return predicate.evaluate(with: dictionary)
}
array.removeObjects(at: indexes)
您可以从here下载游乐场。
答案 1 :(得分:-1)
尝试使用以下代码,这可以解决您的问题。如有任何澄清,请随时发表评论。 :)
//Creating an `NsPredicate` that helps to find out the dictionary which contains 'x' code.
let pred = NSPredicate(format: "coupon_code == %@", x)
//Filtering your array with the pred to get that Dictionary.
let dict = array .filtered(using: pred)
//Deleting that object from your array.
array .removeObject(identicalTo: dict)