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

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

问题描述

我找不到方法的工作示例 [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 。然而,在<一href=\"http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDictionary/getObjects%3aandKeys%3a\">documentation它指出,这种方法的输出变量作为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.

下面是错误消息/警告,如果按照链接的例子,你可能会得到:

Here are the error messages/warnings you might get if you follow the linked example:

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];
}

我将提供一个工作的解决方案作为这个问题的答案。

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);
}

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

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