2012-10-12 52 views
0

即時得到以下錯誤發送指針「的NSString * _strong *類型爲_unsafe_unretained ID參數*」改變保留/釋放性能

Sending "NSString *_strong*to parameter of type _unsafe_unretained id* "changes retain/release properties of pointer ......

以下行

[theDict getObjects:values andKeys:keys]; 我試着從聯繫人添加地址到我的應用程序。有人能向我解釋它的抱怨嗎?我認爲它是一個ARC問題,可能與手動內存管理有關?但我不確定如何解決它。

- (BOOL)peoplePickerNavigationController: 
(ABPeoplePickerNavigationController *)peoplePicker 
    shouldContinueAfterSelectingPerson:(ABRecordRef)person 
          property:(ABPropertyID)property 
          identifier:(ABMultiValueIdentifier)identifier 


{ 
    if (property == kABPersonAddressProperty) { 
    ABMultiValueRef multi = ABRecordCopyValue(person, property); 

    NSArray *theArray = (__bridge id)ABMultiValueCopyArrayOfAllValues(multi); 

    const NSUInteger theIndex = ABMultiValueGetIndexForIdentifier(multi, identifier); 

    NSDictionary *theDict = [theArray objectAtIndex:theIndex]; 

    const NSUInteger theCount = [theDict count]; 

    NSString *keys[theCount]; 

    NSString *values[theCount]; 

    [theDict getObjects:values andKeys:keys]; <<<<<<<<< error here 

    NSString *address; 
    address = [NSString stringWithFormat:@"%@, %@, %@", 
       [theDict objectForKey: (NSString *)kABPersonAddressStreetKey], 
       [theDict objectForKey: (NSString *)kABPersonAddressZIPKey], 
       [theDict objectForKey: (NSString *)kABPersonAddressCountryKey]]; 

    _town.text = address; 

    [ self dismissModalViewControllerAnimated:YES ]; 

     return YES; 
} 
return YES; 
} 

回答

1

對的NSDictionary getObjects文檔:andKeys:其顯示爲:

- (void)getObjects:(id __unsafe_unretained [])objects andKeys:(id __unsafe_unretained [])keys 

但你傳遞這兩個值都很強的NSString引用(局部變量和實例變量默認情況下強這就是爲什麼有在ARC錯誤您的參數不符合預期的類型

更改:

NSString *keys[theCount]; 
NSString *values[theCount]; 

到:

NSString * __unsafe_unretained keys[theCount]; 
NSString * __unsafe_unretained values[theCount]; 

應該解決編譯器問題。

此更改意味着陣列中的任何對象都不會被安全地保留。但只要'theDict'在'keys'和'values'之前沒有超出範圍,那麼你就沒問題了。

+0

這個要求不僅僅是'theDict'不會被釋放,而且任何鍵或值都不會被刪除或改變。 – Dani

+0

@Dani - 是的,很好的說明。 – rmaddy

+0

謝謝你一直堅持這一點。 – JSA986

-1

你是正確的,這是一個錯誤的ARC,它的困惑,你試圖同時分配給NSArrays和NSString的你試圖創建NSString的數組,這我不知道會工作以你打算的方式。

我沒有看到你在以後使用它們,但你會想要做

NSArray *keys, *values; 

擺脫錯誤的。

相關問題