【问题标题】:NSMutableDictionary remove object at key path?NSMutableDictionary 删除关键路径上的对象?
【发布时间】:2013-02-27 06:38:20
【问题描述】:

我有一个分层的 NSMutableDictionary 对象,我希望能够删除层次结构中更深层次的字典。有没有一种快速简便的方法来做到这一点,例如,类似于 removeObjectAtKeyPath 的方法?好像一个也找不到

谢谢!

【问题讨论】:

  • 试试removeValueForKey:

标签: ios objective-c nsmutabledictionary key-value-coding


【解决方案1】:

没有内置任何东西,但您的基本类别方法就可以了:

@implementation NSMutableDictionary (WSSNestedMutableDictionaries)

- (void)WSSRemoveObjectForKeyPath: (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

注意这要求路径的倒数第二个元素——tailContainer 是响应removeObjectForKey: 的东西,可能是另一个NSMutableDictionary。如果不是,请繁荣!

【讨论】:

  • 使用 keyPath 更优雅的方法是使用 NSString pathUtilities
【解决方案2】:

您可以创建一个类别:

这最多向下一级:

#import "NSMutableDictionary+RemoveAtKeyPath.h"

@implementation NSMutableDictionary (RemoveAtKeyPath)

-(void)removeObjectAtKeyPath:(NSString *)keyPath{

    NSArray *paths=[keyPath componentsSeparatedByString:@"."];

    [[self objectForKey:paths[0]] removeObjectForKey:paths[1]];

}

@end

它被称为:

NSMutableDictionary *adict=[[NSMutableDictionary alloc]initWithDictionary:@{@"key1" : @"obj1", @"key11":@"obj11"}];

NSMutableDictionary *bdict=[[NSMutableDictionary alloc]initWithDictionary:@{@"key2" : adict}];

NSLog(@"%@",bdict);
NSLog(@"%@",[bdict valueForKeyPath:@"key2.key1"]);

[bdict removeObjectAtKeyPath:@"key2.key1"];
NSLog(@"After category : %@",bdict);

【讨论】:

    【解决方案3】:

    为了处理不包含句点的键路径(即实际上是键的键路径),对 Josh 的答案进行了小幅改进:

    - (void)removeObjectAtKeyPath:(NSString *)keyPath
    {
        NSArray *keyPathElements = [keyPath componentsSeparatedByString:@"."];
        NSUInteger numElements = [keyPathElements count];
        if (numElements == 1) {
            [self removeObjectForKey:keyPath];
        } else {
            NSString *keyPathHead = [[keyPathElements subarrayWithRange:(NSRange){0, numElements - 1}] componentsJoinedByString:@"."];
            NSMutableDictionary *tailContainer = [self valueForKeyPath:keyPathHead];
            [tailContainer removeObjectForKey:[keyPathElements lastObject]];
        }
    }
    

    【讨论】:

      【解决方案4】:

      我知道这是一篇较旧的帖子,但我需要在 Swift 2.0 中找到相同的解决方案,无法找到简单的答案我想出了这个解决方案:

      public extension NSMutableDictionary {
          public func removeObjectAtKeyPath(keyPath:String) {
              let elements = keyPath.componentsSeparatedByString(".")
              let head = elements.first!
              if elements.count > 1 {
                  let tail = elements[1...elements.count-1].joinWithSeparator(".")
                  if let d = valueForKeyPath(head) as? NSMutableDictionary {
                      d.removeObjectAtKeyPath(tail)
                  }
              }else{
                  removeObjectForKey(keyPath)
              }
          }
      }
      

      我使用递归向NSMutableDictionary 添加了一个扩展,以逐步通过keyPath

      【讨论】:

        【解决方案5】:

        我只是在 jscs 接受的答案上进行了迭代,并稍作改进 - 使用 NSString 的内置 pathUtlities 类别处理 KVC 密钥路径。

        @implementation NSMutableDictionary (NestedMutableDictionaries)
        - (void)removeObjectForKeyPath: (NSString *)keyPath
        {
            NSArray *key = [keyPath pathExtension];
            NSString *keyPathHead = [keyPath stringByDeletingPathExtension];
            [[self valueForKeyPath:keyPathHead] removeObjectForKey:key];
        }
        @end
        

        我觉得稍微好一点。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-11-02
          • 1970-01-01
          • 2014-03-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-03
          相关资源
          最近更新 更多