2013-08-19 68 views
-2

我想要使用此代碼,我在這個崗位發現的NSArray轉換爲NSDictionary中: Convert NSArray to NSDictionary轉換NSArray的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 

但是存在[自獲得對象的行錯誤:數組對象,...],並帶消息:發送「NSString _strong到_unsafe_unretained id *類型的參數」更改指針的保留/釋放屬性。我想這是因爲ARC的問題,因爲該職位是在2009年 任何人都知道如何擺脫這個問題感謝..

+0

什麼是[self count]?自我?通過NSLog在控制檯中顯示它?和http://stackoverflow.com/questions/12868603/sending-nsstring-strongto-parameter-of-type-unsafe-unretained-id-change-r – 2013-08-19 04:34:53

+0

http://pastebin.com/YWYbStxK – 2013-08-19 04:43:17

+2

什麼是創造無論如何,密鑰只是索引的字典?沒有我能看到的。 – trojanfoe

回答

0

的解決方案几乎是一樣的Ranju帕特爾上面給出的鏈接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"中進行了說明。

+0

謝謝,作品像魅力.. – user2373192