2011-12-16 66 views
1

對於objective-c和ios開發來說相當新,我想弄清楚如何從Address Book Programming Guide for iOS實現地址簿代碼。使用ios5與ARC選擇地址簿地址

我希望能夠記錄用戶在地址簿中點擊的地址。

我設法在我的應用程序中實現地址簿。

iOS4的下面「作品」:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker 
     shouldContinueAfterSelectingPerson:(ABRecordRef)person 
           property:(ABPropertyID)property 
           identifier:(ABMultiValueIdentifier)identifier 
{ 
    if (property == kABPersonAddressProperty) // if tapped is equal to a phone property 
    { 
     NSLog(@"Address tapped!"); 
     NSMutableDictionary * cfaddress; 
     ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty); 
     for(CFIndex i = 0; i < ABMultiValueGetCount(address); i++) { 
      if(identifier == ABMultiValueGetIdentifierAtIndex (address, i)) { //if tapped number identifier is the same as identifier number tapped 
       cfaddress = ABMultiValueCopyValueAtIndex(address, i); // copy the number to CFSTRING number 
      } 
     }   

     NSLog(@"%@", [cfaddress objectForKey:@"City"]);  
     NSLog(@"%@", [cfaddress objectForKey:@"Street"]); 

     // City and street present ? 

     if([cfaddress objectForKey:@"City"] && [cfaddress objectForKey:@"Street"]){ 
      NSLog(@"STAD EN STRAAT"); 
     } 
     else{ 
      NSLog(@"GEEN STAD EN/OF STRAAT"); 
     } 
    } 

    [self dismissModalViewControllerAnimated:YES]; 
    return YES; 
} 

不幸的是在這個自動引用計數的iOS5給了我以下錯誤:

error: Automatic Reference Counting Issue: Implicit conversion of a non-Objective-C pointer type 'CFTypeRef' (aka 'const void *') to 'NSMutableDictionary *' is disallowed with ARC

我猜我這樣做的方式(對於ios4)反正不是一個好方法,我真的很感激有關如何實現這一點的一些建議。提前致謝!

+0

添加投地'的NSMutableDictionary *`或使用自動釋放構造函數之一是更加明確。 – Hyperbole 2011-12-16 16:49:58

+0

你能否用例子解釋兩個選項? – Laurens 2011-12-16 17:08:29

回答

2

repalace這

cfaddress = ABMultiValueCopyValueAtIndex(address, i); 

與此

cfaddress = [NSMutableDictionary dictionaryWithDictionary:(NSDictionary *)ABMultiValueCopyValueAtIndex(address, i)];