【问题标题】:Convert NSArray to NSDictionary - Error [closed]将 NSArray 转换为 NSDictionary - 错误 [关闭]
【发布时间】:2013-08-19 04:30:26
【问题描述】:

我正在尝试使用我在这篇文章中找到的代码将 NSArray 转换为 NSDictionary: Convert NSArray to NSDictionary

@implementation NSArray (indexKeyedDictionaryExtension)

- (NSDictionary *)indexKeyedDictionary
{
NSUInteger arrayCount = [self count];
id arrayObjects[arrayCount], objectKeys[arrayCount];

[self getObjects:arrayObjects range:NSMakeRange(0UL, arrayCount)];
for(NSUInteger index = 0UL; index < arrayCount; index++) { objectKeys[index] = [NSNumber numberWithUnsignedInteger:index]; }

return([NSDictionary dictionaryWithObjects:arrayObjects forKeys:objectKeys count:arrayCount]);
}

@end

但是在 [self get objects:array objects,...] 行中存在错误,带有消息:Sending “NSString _strongto parameter of type _unsafe_unretained id* ”change retain/释放指针的属性。我认为这是因为 ARC 问题,因为该帖子是在 2009 年。 有人知道如何解决这个问题吗?谢谢..

【问题讨论】:

  • 什么是[自计数]??自己 ??通过 NSLog 在控制台中显示它?和stackoverflow.com/questions/12868603/…
  • 创建一个关键字就是索引的字典有什么好处?我看不到。
  • 我尝试了 pastebin 链接,但在这一行仍然出错:[self getObjects:arrayObjects range:NSMakeRange(0UL, arrayCount)];

标签: ios objective-c nsdictionary


【解决方案1】:

解决方案与上面 Ranju Patel 给出的链接Sending "NSString *_strong*to parameter of type _unsafe_unretained id* "change retain/release properties of pointer 中的几乎相同:

__unsafe_unretained id arrayObjects[arrayCount];
id objectKeys[arrayCount];
[self getObjects:arrayObjects range:NSMakeRange(0UL, arrayCount)];

因为getObject:range: 被声明为

- (void)getObjects:(id __unsafe_unretained [])objects range:(NSRange)range;

原因是 ARC 无法管理纯 C 数组中对象的生命周期, 因此必须将对象声明为__unsafe_unretained。 这也在“转换项目时的常见问题”中进行了说明 在"Transitioning to ARC Release Notes".

【讨论】:

  • 谢谢,很有魅力..
猜你喜欢
  • 2010-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-11
相关资源
最近更新 更多