2011-11-06 88 views
2

感謝這個問題/答案Automatic Reference Counting: Error with fast enumeration我解決了一部分讓我瘋狂的東西。快速枚舉NSDictionary的錯誤

不過,我仍然得到一個錯誤「分配‘的NSDictionary * __強’到‘__unsafe_unretained ID’改變保留指針的/釋放屬性」。我已經閱讀了ARC編程指南和與指針轉換相關的部分,但不幸的是,我還不是一個好的c程序員。

查看帶註釋的代碼行。任何幫助將非常感激。提前致謝。

- (NSUInteger) countByEnumeratingWithState: (NSFastEnumerationState *)state 
           objects: (id __unsafe_unretained *)buffer 
           count: (NSUInteger)bufferSize 
{ 
    if (*enumRows = [self getPreparedRow]) { 
     state->itemsPtr = enumRows; // Assigning 'NSDictionnary *__strong *' to '__unsafe_unretained id*' changes retain/release properties of pointer 
     state->state = 0; 
     state->mutationsPtr = &state->extra[0]; // was state->mutationsPtr = (unsigned long *) self; before 
     return 1; 
    } else { 
     return 0; 
    } 
} 

本宣言所.h文件

@interface BWDB : NSObject <NSFastEnumeration> { 
    NSDictionary * enumRows[1]; 
} 
+2

'id'已經是一個指針。 –

回答

2

這將解決這個問題。

@interface BWDB : NSObject <NSFastEnumeration> { 
    __unsafe_unretained NSDictionary *enumRows[1]; 
} 

由您發佈,這應該是絕對安全的,因爲你每一次分配它,你檢查一下之前的代碼。事實上,根本沒有必要將這個實例變量。

你可能只是這樣做:

{ 
    __unsafe_unretained NSDictionary *enumRows[1]; 
    if ((*enumRows = [self getPreparedRow])) { 
     state->itemsPtr = NULL; 
     state->itemsPtr = enumRows; 
     state->state = 0; 
     state->mutationsPtr = &state->extra[0]; 
     return 1; 
    } else { 
     return 0; 
    } 
} 

我要指出,沒有具有NSDictionaries的指針[]數組類型太大的價值。簡單地使用一個NSArray來保存你的NSDictionaries會更有優勢。

+0

非常感謝,它做到了,我很感激。 –

+0

@brad很高興爲您提供幫助。如果答案解決了你的問題,你應該接受它。 – NJones