【问题标题】:search element in NSDictionary nested in another NSDictionary嵌套在另一个 NSDictionary 中的 NSDictionary 中的搜索元素
【发布时间】:2017-02-13 07:42:43
【问题描述】:

背景:

一个对象:

MyObject : NSObject
@property NSInteger type;

一个 NSDictionary:

@{
@"1":@{@"1":MyObject}
@"2":@{@"1":MyObject}
}

现在,得到了一个 MyObject,它的 type=1; 如何确定这个 MyObject 是否包含在 NSDictionary 中?

【问题讨论】:

标签: ios nsdictionary


【解决方案1】:

我正在考虑,您的字典只有一个键/对,并且没有类似的键名存在

  NSDictionary *dict = @{ @"a":@{@"c":@"MyObject"}                                        ,@"b":@{@"d":@"MyObject"} } ;

NSMutableArray *valuesArray = [[NSMutableArray alloc]init];
 // enumerate and add all values to our value array
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    NSLog(@"Your value : %@", [obj description]);

    [valuesArray addObject:[[obj allValues] firstObject]];
}];

// check whether this value is present
if ([valuesArray containsObject:@"MyObject"]) {
    NSLog(@"My Object found",);
}

【讨论】:

  • @{ @"a":@{@"c":MyObject} @"b":@{@"d":MyObject} }
  • 您要检查 myObject 是否存在?
猜你喜欢
  • 2014-04-04
  • 2012-07-30
  • 1970-01-01
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多