[NSDictionary getObjects:andKeys:] 的示例

发布于 2024-09-02 15:00:29 字数 1275 浏览 3 评论 0原文

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

我感到困惑的原因是 NSDictionary 上的大多数方法都返回一个 NSArray。但是,在 文档 它指出此方法的输出变量以 C 数组形式返回。

以下是如果您按照链接示例操作可能会收到的错误消息/警告:

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 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.

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.

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

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

发布评论

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

评论(2

筱武穆 2024-09-09 15:00:29

以下是使用此方法的正确方法:

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

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);
}
桃扇骨 2024-09-09 15:00:29

在 ARC 下,解决方案需要修改如下(__unsafe_unretained 添加到数组定义中):

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

NSInteger count = [myDictionary count];
id __unsafe_unretained objects[count];
id __unsafe_unretained 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);
}

Under ARC the solution needs to be modified as follows (__unsafe_unretained added to the array definitions):

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

NSInteger count = [myDictionary count];
id __unsafe_unretained objects[count];
id __unsafe_unretained 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文