使用 NSPredicate 替换 Array 中的 Dictionary 对象

Replace an Dictionary object in Array using NSPredicate

我在数组中有一个字典对象。

我想用新字典替换这个对象。

两者具有相同的 order_id。

目前我正在这样做,我如何使用 NSPredicate 来做到这一点。

NSMutableArray *orderList=[[NSUserDefaults standardUserDefaults]  objectForKey:@"Orders"];

        //2. Find and replace the object/OrdeDetails.
        for(int i=0;i<orderList.count;i++){

            NSDictionary *dictionary=orderList[i];

            if([dictionary[@"order_id"] isEqualToString:OrderDetails[@"order_id"]]){

                [orderList replaceObjectAtIndex:i withObject:OrderDetails];

                break;
            }


        }

您不能用 NSPredicate 替换对象,但是您可以搜索它然后再进行替换。

好吧,这是未经测试而编造的,但我确实认为它具有有效的语法。

您可以以此为基础,或者希望您掌握使用谓词的逻辑。

// construct the predicate with the given condition
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"order_id = %@",dictionary[@"order_id"]];

// this will filter the array according to the given predicate. if there are more than 1 entries with the given condition then you should handle that this only handle unique entries
NSArray *array = [orderList filteredArrayUsingPredicate:predicate];

// assuming that order_id is unique
NSInteger index = [orderList indexOfObject:[array lastObject]];
if (index != NSNotFound) // check if index is existing
  [orderList replaceObjectAtIndex:index withObject:orderDetails]; // replace the object with the desired object

检查此代码:

 NSPredicate *resultPredicate = [NSPredicate
                                        predicateWithFormat:@"SELF.order_id contains[cd] %@",OrderDetails[@"order_id"]];
 NSMutableArray *arrOrders = [[NSMutableArray alloc]init];
    [arrOrders addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"Orders"]];

 NSArray *filteredOrder = [arrOrders filteredArrayUsingPredicate:resultPredicate];

 if ([filteredOrder count] > 0) {
       NSUInteger index = [arrOrders indexOfObject:[filteredOrder objectAtIndex:0]];
       if (index != NSNotFound) {
           [arrOrders replaceObjectAtIndex:index withObject:OrderDetails];
       }
}

如果我想完美匹配,那么我使用了 LIKE。虽然我不确定。 来自 linkcheat sheet

    NSPredicate *predicateSearchOrder=[NSPredicate predicateWithFormat:@"SELF.order_id LIKE[cd] %@",[responsedDict valueForKey:@"order_id"]];
            //
            //        //It will be only one object with order id.

NSArray *searchedArray=[orderList filteredArrayUsingPredicate:predicateSearchOrder];

if(searchedArray.count>0){
            NSDictionary *toModiFyDictionary=searchedArray[0];
            toModiFyDictionary=OrderDetails;
}

这也有效

如果你用的是Swift,就很简单了。

public class Test : NSObject {
    class func test() -> Void {
        var array : [Dictionary<String, String>] = []

        let dic: Dictionary<String, String> = ["order_id" : "111", "name" : "good1"]
        let dic2: Dictionary<String, String> = ["order_id" : "222", "name" : "good2"]

        let dic3: Dictionary<String, String> = ["order_id" : "111", "name" : "good3"]
        array.append(dic)
        array.append(dic2)

        let result = array.map { (elem) -> [String : String] in
            if elem["order_id"] == "111" {
                return dic3
            }
            return elem
        }
        print("result = \(result)") 
    }
}