0

我有一个包含字典数组的 plist 文件在此处输入图像描述

我如何遍历它并使用键 fQty 删除所有键和值?我需要删除 fQty 以便我可以将字典与不考虑 fQty 字段的新字典进行比较。

4

2 回答 2

0

这有点粗糙,但也许你可以使用这样的东西:

-(BOOL)isObjectAdictionary:(id)object{
    if([object isKindOfClass:[NSMutableDictionary class]]||[object isKindOfClass:[NSDictionary class]]){
    return YES;
    }
    return NO;
}

 -(BOOL)isObjectAnArray:(id)object{
      if([object isKindOfClass:[NSMutableArray class]]||[object isKindOfClass:[NSArray class]]){
         return YES;
      }
     return NO;
   }


-(id)loop:(id)object removeObjectWithKey:(NSString *)key{

if([self isObjectAdictionary:object]){
    NSMutableDictionary *mutableObject = [NSMutableDictionary dictionaryWithDictionary:object];
    if([[mutableObject allKeys]containsObject:key]){
        [mutableObject removeObjectForKey:key];
        object = mutableObject;
        return object;
    }

}

if([object respondsToSelector:@selector(count)]){


    if([self isObjectAdictionary:object]){
        NSMutableDictionary *objects_copy = [NSMutableDictionary dictionaryWithDictionary:object];
        for(NSString *objects_key in object){

            id tempObject = [object valueForKey:objects_key];
            tempObject = [self loop:tempObject removeObjectWithKey:key];
            [objects_copy setObject:tempObject forKey:objects_key];
        }
        object =(id)objects_copy;
    }
    else if([self isObjectAnArray:object]){//an array
        NSMutableArray *objects_copy = [NSMutableArray arrayWithArray:object];
        for(int i = 0;i<[object count];i++){
            id tempObject = [object objectAtIndex:i];
           tempObject = [self loop:tempObject removeObjectWithKey:key];
            [objects_copy replaceObjectAtIndex:i withObject:tempObject];
        }
        object = (id)objects_copy;
    }

}
 return object;
}

你可以这样使用:[self loop:yourArray removeObjectWithKey:@"fQty"]);

于 2013-08-28T07:56:30.870 回答
0

尝试这个:

(假设您的 pilst 文件名为:data.plist)

- (void) deal:(id)item
{
    if ([item isKindOfClass:[NSDictionary class]]) {
        for (id key in item) {
            id it = [item objectForKey:key];
            if ([it isKindOfClass:[NSDictionary class]] || [it isKindOfClass:[NSArray class]]) {
                [self deal:it];
            }
        }
        if ([item objectForKey:@"fQty"]!=nil) {
            [item setObject:@"0" forKey:@"fQty"];
        }
    }
    if ([item isKindOfClass:[NSArray class]]) {
        for (id it in item) {
            if ([it isKindOfClass:[NSDictionary class]] || [it isKindOfClass:[NSArray class]]) {
                [self deal:it];
            }
        }
    }
}

{
    NSError** error = nil;
    NSPropertyListFormat format;
    NSArray *dma = (NSMutableArray *)[NSPropertyListSerialization
                                             propertyListWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"XD" ofType:@"plist"]]
                                            options:NSPropertyListMutableContainersAndLeaves
                                             format:&format error:error];

    [self deal:dma];
}

现在 myArray 就是你想要的。

编辑:您想将键 @"fQty" 的值设置为 0,

将 for 循环中的行更改为 [item setObject:@"0" forKey:@"fQty"],就是这样。

再次编辑:用递归方式解决问题。

于 2013-08-28T07:47:45.053 回答