[NSDictionary getObjects:andKeys:]的示例 [英] Example of [NSDictionary getObjects:andKeys:]

查看:219
本文介绍了[NSDictionary getObjects:andKeys:]的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到方法 [NSDictionary getObjects:andKeys:] 的工作示例。我唯一可以找到的示例编译。我在这里提供了错误/警告,以防有人在搜索它们。

I couldn't find a working example of the method [NSDictionary getObjects:andKeys:]. The only example I could find, doesn't compile. I provided the errors/warnings here in case someone is searching for them.

我困惑的原因是因为NSDictionary上的大多数方法返回一个 NSArray 。但是,在文档它声明这个方法的输出变量作为C数组返回。

The reason I was confused is because most methods on NSDictionary return an NSArray. However, in the documentation it states that the out variables of this method are returned as C arrays.

NSDictionary *myDictionary = ...;

id objects[]; // Error: Array size missing in 'objects'
id keys[]; // Error: Array size missing in 'keys'

[myDictionary getObjects:&objects andKeys:&keys];

for (int i = 0; i < count; i++) {
  id obj = objects[i];
  id key = keys[i];
}

NSDictionary *myDictionary = ...;

NSInteger count = [myDictionary count];
id objects[count];
id keys[count];
[myDictionary getObjects:&objects andKeys:&keys]; // Warning: Passing argument 1 of 'getObjects:andKeys:' from incompatible pointer type.

for (int i = 0; i < count; i++) {
  id obj = objects[i];
  id key = keys[i];
}



我将提供一个有效的解决方案作为此问题的答案。 p>

I'll provide a working solution as an answer to this question.

推荐答案

这是使用此方法的正确方法:

Here's the correct way to use this method:

NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"A", @"2", @"B", nil];

NSInteger count = [myDictionary count];
id objects[count];
id keys[count];
[myDictionary getObjects:objects andKeys:keys];

for (int i = 0; i < count; i++) {
  id obj = objects[i];
  id key = keys[i];
  NSLog(@"%@ -> %@", obj, key);
}

这篇关于[NSDictionary getObjects:andKeys:]的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆