removeObjectForKey不会从.plist中删除值

时间:2014-06-19 07:13:47

标签: ios uitableview plist

我正在开发iPhone应用程序,其中我有TableView,

我想要做的是点击UITableViewCell我想从plist,MOBILES.Brand.2MOBILES.Brand.5删除这些值,如果它们存在于.plist中,如果它们不存在于.plist中那么我想把它添加到我的.plist

这是我的.plist的结构:

: 这是我的代码片段:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       plistPath = [self getPlistPath];

//PlistDict is mutableDictionary contains the keys and values of .plist (of above image) 
if ([PlistDict valueForKeyPath:@"MOBILES.Brand.2"] != nil) { //if key and it's value exists in Filter.plist

//Delete Key from .plist...
                NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
                [savedStock removeObjectForKey:@"MOBILES.Brand.2"];
                [savedStock removeObjectForKey:@"MOBILES.Brand.5"];
                [savedStock writeToFile:plistPath atomically:YES];

}else{



//Add Key to .plist
            NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
            [data setValue:@"251" forKeyPath:@"MOBILES.Brand.2"];
            [data setValue:@"298" forKeyPath:@"MOBILES.Brand.5"];
            [data writeToFile:plistPath atomically:YES];

    }
}

-(NSString*)getPlistPath{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Filter.plist"];
    PlistDict = [NSMutableDictionary dictionaryWithContentsOfFile:path];

    return path;
}

在编写上面的代码片段后,它不会从.plist中删除密钥,但如果不存在则会成功添加密钥。

我在哪里做错了?请提前帮助和感谢。

1 个答案:

答案 0 :(得分:3)

问题是,您尝试使用NSMutableDictionary keypath直接使用removeObjectForKey删除exact key而不是keypath而您正在提供keypath。使用here

中的以下NSMutableDictionary Category
@interface NSMutableDictionary (Additions)
- (void)removeObjectForKeyPath: (NSString *)keyPath;
@end

@implementation NSMutableDictionary (Additions)

- (void)removeObjectForKeyPath: (NSString *)keyPath
{
    // Separate the key path
    NSArray * keyPathElements = [keyPath componentsSeparatedByString:@"."];
    // Drop the last element and rejoin the path
    NSUInteger numElements = [keyPathElements count];
    NSString * keyPathHead = [[keyPathElements subarrayWithRange:(NSRange){0, numElements - 1}] componentsJoinedByString:@"."];
    // Get the mutable dictionary represented by the path minus that last element
    NSMutableDictionary * tailContainer = [self valueForKeyPath:keyPathHead];
    // Remove the object represented by the last element
    [tailContainer removeObjectForKey:[keyPathElements lastObject]];
}

@end

它应该适合你。

方法 - 2

如果上述方法无效,您可以对字典进行迭代,并专门删除密钥的对象。尝试

NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];

NSMutableDictionary *brand=[[savedStock objectForKey:@"MOBILES"] objectForKey:@"Brand"];

[brand removeObjectForKey:@"2"];
[brand removeObjectForKey:@"5"];

干杯。