使用NSPredicate替换Array中的Dictionary对象

时间:2016-12-30 07:08:54

标签: ios objective-c iphone nsdictionary nspredicate

我在Array中有一个字典对象。

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

两者都有相同的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;
            }


        }

4 个答案:

答案 0 :(得分:1)

您无法用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

答案 1 :(得分:1)

检查此代码:

 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];
       }
}

答案 2 :(得分:0)

如果我想匹配完美,那么我就用了,喜欢。虽然我不确定。 来自链接cheat 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;
}

这也有效

答案 3 :(得分:0)

如果你使用的是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)") 
    }
}