Ios 使用NSPredicate替换数组中的字典对象

Ios 使用NSPredicate替换数组中的字典对象,ios,objective-c,iphone,nsdictionary,nspredicate,Ios,Objective C,Iphone,Nsdictionary,Nspredicate,我在数组中有一个dictionary对象 我想用新字典替换这个对象 两者都有相同的订单号 目前,我正在这样做,我如何才能与NSPredicate做到这一点 NSMutableArray *orderList=[[NSUserDefaults standardUserDefaults] objectForKey:@"Orders"]; //2. Find and replace the object/OrdeDetails. for(int i=0;i<o

我在数组中有一个dictionary对象

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

两者都有相同的订单号

目前,我正在这样做,我如何才能与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;
            }


        }
NSMutableArray*orderList=[[NSUserDefaults standardUserDefaults]objectForKey:@“Orders”];
//2. 查找并替换对象/或详细信息。

对于(int i=0;i您不能将对象替换为
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
,但可以先搜索对象,然后再进行替换

我只是在没有测试的情况下编造了这个,但我确实认为它有一个有效的语法

你可以用这个作为基础,或者希望你能在谓词中找到逻辑。

// 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];
       }
}
检查此代码:

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

如果我想匹配完美,那么我就用,比如。虽然我不确定。 从链接中获取


如果我想匹配完美,那么我就使用了,比如。虽然我不确定。 从链接中获取


如果您使用的是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)") 
    }
}
公共类测试:NSObject{
类func test()->Void{
变量数组:[字典]=[]
让dic:Dictionary=[“订单id”:“111”,“名称”:“good1”]
让dic2:Dictionary=[“order_id”:“222”,“name”:“good2”]
让dic3:Dictionary=[“order_id”:“111”,“name”:“good3”]
数组.附加(dic)
array.append(dic2)
让result=array.map{(elem)->[String:String]in
如果元素[“订单id”]=“111”{
返回dic3
}
返回元素
}
打印(“结果=\(结果)”)
}
}

如果您使用的是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)") 
    }
}
公共类测试:NSObject{
类func test()->Void{
变量数组:[字典]=[]
让dic:Dictionary=[“订单id”:“111”,“名称”:“good1”]
让dic2:Dictionary=[“order_id”:“222”,“name”:“good2”]
让dic3:Dictionary=[“order_id”:“111”,“name”:“good3”]
数组.附加(dic)
array.append(dic2)
让result=array.map{(elem)->[String:String]in
如果元素[“订单id”]=“111”{
返回dic3
}
返回元素
}
打印(“结果=\(结果)”)
}
}

如果上述方法有效,为什么要使用NSPredicate?您不能将对象替换为
NSPredicate
,您可以简单地查找该对象。是的,我认为查找时可以使用NSPredicate。因此,我们可以跳过几行for循环。但我认为for循环很好。如果上述方法有效,为什么要使用NSPredicate进行此操作s?你不能用
NSPredicate
替换对象,你可以简单地找到该对象。是的,我认为我们可以使用NSPredicate进行查找。因此,我们可以跳过几行for循环。但是我认为for循环是好的。是的,Joshua,这是一种方法,但有点棘手,否则for循环是好的。谢谢@Joshua,这不是很棘手,但只需要下一步站在谓词如何工作的立场上。我认为这是了解NSPredicate的一个好方法。所有答案看起来都一样,我的答案需要有一些指针理解,但我仍将使用for循环。它不那么复杂。是的,Joshua,这是一种方法,但有点棘手,否则for循环是好的。谢谢@Joshua不是很棘手,但只需要了解predicate有效。我认为这是了解NSPredite的一个好方法。所有答案看起来都一样,我的答案需要有一些指针理解,但我仍将使用for循环。它不太复杂。比我的答案更合适。比我的答案更合适。本质上,它也是for循环本质上,它也是for循环