使用字典中字典中的对象填充 mutableArray

发布于 2024-10-13 07:40:59 字数 792 浏览 5 评论 0 原文

我在从另一个 NSDictionary *dictionary(人员)内的一组 NSDictionaries(每个代表一个员工)填充对象数组(员工姓名)时遇到问题。每个子词典的键是员工 ID。

我很可能是从一个完全错误的方向来的,我是一个新手,但从我读到的内容来看应该是这样的,我已经设法从每个子词典中获取 pName(员工姓名) ,一切都转瞬即逝。我只是无法将 pName 添加到 MutableArray *names 中。

任何帮助或指导将不胜感激......

  int i, count;
  id key, value;

  keys = [dictionary allKeys];
  count = [keys count];
  for (i = 0; i < count; i++)
  {
   key = [keys objectAtIndex: i];
   value = [dictionary objectForKey: key];
   NSLog (@"value:%@", value); 

   NSString *pName = [value objectForKey:@"personName"];
   NSLog (@"pName:%@", pName);// the debugger shows the correct pName with each loop

   [names addObject:pName];// this is wrong and i don't know why
  }
   NSLog (@"names:%@", names);// in the debugger names (null)

I am having trouble populating an array of objects (employee names) from a group of NSDictionaries (each one representing an individual employee) which are inside another NSDictionary *dictionary (personnel). The key for each sub Dictionary is an employee ID.

I might well be coming at this from a completely wrong direction, i am very much a newbie but from what i have read it should be something like this , i have managed to get as far as a pName (employee name) from each sub Dictionary, all be it fleetingly. I just can't get the pNames added into MutableArray *names.

any help or guidance would be greatly appreciated ...

  int i, count;
  id key, value;

  keys = [dictionary allKeys];
  count = [keys count];
  for (i = 0; i < count; i++)
  {
   key = [keys objectAtIndex: i];
   value = [dictionary objectForKey: key];
   NSLog (@"value:%@", value); 

   NSString *pName = [value objectForKey:@"personName"];
   NSLog (@"pName:%@", pName);// the debugger shows the correct pName with each loop

   [names addObject:pName];// this is wrong and i don't know why
  }
   NSLog (@"names:%@", names);// in the debugger names (null)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏雪无痕 2024-10-20 07:40:59

您还可以使用 集合运算符,特别是 @unionOfObjects 运算符:

NSMutableArray *names = [[[personnel allValues] 
                            valueForKeyPath:@"@unionOfObjects.personName"] 
                            mutableCopy];

请注意,names 将归该方法所有(其保留计数为 1),但临时数组([personnel allValues] 和由 valueForKeyPath: 创建的数组)会自动释放,并且不属于该方法。

由于某种原因(可能是因为以“@”开头的键名在 valueForKey:,因此 valueForKeyPath: 不会相当于对 valueForKey: 的调用序列),NSDictionary 没有 valueForKeyPath: 方法,因此您首先需要将其转换为使用 allValues 的数组。或者,您可以在类别中实现 valueForKeyPath:,也许从 GnuStep cocotron 的(实际上并不支持@unionOfObjects,但允许通过定义_kvo_operator_unionOfObjects:方法轻松扩展)。

You can also do this using collection operators, specifically the @unionOfObjects operator:

NSMutableArray *names = [[[personnel allValues] 
                            valueForKeyPath:@"@unionOfObjects.personName"] 
                            mutableCopy];

Note that names will be owned by the method (its retain count is 1), but the interim arrays ([personnel allValues] and the one created by valueForKeyPath:) are autoreleased and not owned by the method.

For some reason (possibly because key names beginning with "@" are special in valueForKey:, and thus valueForKeyPath: wouldn't be equivalent to a sequence of calls to valueForKey:), NSDictionary doesn't have a valueForKeyPath: method, so you first need to convert it to an array using allValues. Alternatively, you could implement valueForKeyPath: in a category, perhaps starting from GnuStep's or cocotron's (which doesn't actually support @unionOfObjects, but allows for easy extension by defining a _kvo_operator_unionOfObjects: method).

征棹 2024-10-20 07:40:59

看起来您没有创建一个数组对象来放入您的名字。

names = [NSMutableArray array];

在循环开始之前需要有这样的代码: ... 。这会创建一个空的可变数组。

It looks like you aren't creating an array object to put your names in. There needs to be code like this:

names = [NSMutableArray array];

... before the start of the loop. That creates an empty mutable array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文