gpt4 book ai didi

ios - removeObjectForKey 不会从 .plist 中删除值

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:04:35 25 4
gpt4 key购买 nike

我正在开发具有 TableView 的 iPhone 应用程序,

我想做的是点击 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 中删除键,但如果不存在,它会成功添加键。

我哪里做错了?请帮助并提前致谢。

最佳答案

问题是您正在尝试使用 NSMutableDictionary keypath 直接使用 removeObjectForKey 进行删除,这需要 exact key 而不是 keypath 并且您正在提供 keypath。使用以下取自 hereNSMutableDictionary 类别

@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"];

干杯。

关于ios - removeObjectForKey 不会从 .plist 中删除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24300808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com