[NSDictionary getObjects:andKeys:] 的示例
我找不到方法 [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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是使用此方法的正确方法:
Here's the correct way to use this method:
在 ARC 下,解决方案需要修改如下(__unsafe_unretained 添加到数组定义中):
Under ARC the solution needs to be modified as follows (__unsafe_unretained added to the array definitions):