2011-08-03 28 views
10

Me gustaría obtener una lista de todas las propiedades de un ABPersonRef y ABGroupRef sin tener que usar las claves predefinidas de iOS de kABPersonFirstNameProperty, kABPersonLastNameProperty ... Estoy jugando con la libreta de direcciones y me gustaría iterar sobre todas valores para una persona en particular. Sé que hay teclas predefinidas pero Apple muy bien podría añadir otros nuevos en el futuro, por lo que me gustaría hacer algo como:¿Cómo puedo obtener una lista de todas las propiedades de un ABRecordRef?

ABAddressBookRef addressBook = ABAddressBookCreate(); 
NSArray *allPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook); 
for (int i = 0; i < [allPeople count]; ++i) { 
    ABRecordRef person = [allPeople objectAtIndex:i]; 

    // This is the line that I can't figure out. 
    NSArray *allProperties = (NSArray *)ABRecordCopyArrayOfAllProperties(person); 
} 

sé que vas a encontrar artículos de varios valores que voy a tener un bucle aunque más tarde, pero el objetivo es obtener una lista de claves que pueda iterar para las propiedades de valor único. No me importa cuál sea la clase devuelta, NSArray, NSDictionary ... lo que sea.

Agradezco mucho cualquier consejo!

Respuesta

8

Usted puede intentar lo siguiente:

Con ARC:

NSDictionary* dictionaryRepresentationForABPerson(ABRecordRef person) 
{ 
    NSMutableDictionary* dictionary = [NSMutableDictionary dictionary]; 

    for (int32_t propertyIndex = kABPersonFirstNameProperty; propertyIndex <= kABPersonSocialProfileProperty; propertyIndex ++) 
    { 
     NSString* propertyName = CFBridgingRelease(ABPersonCopyLocalizedPropertyName(propertyIndex)); 
     id value = CFBridgingRelease(ABRecordCopyValue(person, propertyIndex)); 

     if (value) 
      [dictionary setObject:value forKey:propertyName]; 
    } 

    return dictionary; 
} 
  • Nos utilizando el nombre localizado de la propiedad - en diferentes lugares tendrán diferentes teclas.
  • El número de propiedades puede cambiar en la próxima versión de iOS.

Tal vez tenga sentido para ir a través de la serie de propiedades, siempre que el propertyName no se convierta en UNKNOWN_PROPERTY

+0

Su código genera una queja del analizador: "potenciales de fuga de un objeto almacenado en 'propertyName' "y" Posible fuga de un objeto almacenado en 'value' "¿Alguna idea de cómo resolverlo? – MiQUEL

+0

¿Utiliza ARC? –

+0

Sí, he trasladado todos mis proyectos a ARC. – MiQUEL

2

solución de Aliaksandr no es seguro: por ejemplo, si se intenta crear registros ABPerson en un ABSource específica y utilice este enfoque, es posible que los contactos no se sincronicen correctamente con esa fuente.

simplemente copié la lista de 25 ABPropertyIDs de ABPerson, las metió en un simple int [], y repiten a lo largo de ellos ...

 // Loop over all properties of this Person 

     // taken from Apple's ABPerson reference page on 9.12.13. 
     // URL: https://developer.apple.com/library/ios/documentation/AddressBook/Reference/ABPersonRef_iPhoneOS/Reference/reference.html#//apple_ref/c/func/ABPersonGetTypeOfProperty 

     // count = 25. All are type ABPropertyID 

     int propertyArray[25] = { 
      kABPersonFirstNameProperty, 
      kABPersonLastNameProperty, 
      kABPersonMiddleNameProperty, 
      kABPersonPrefixProperty, 
      kABPersonSuffixProperty, 
      kABPersonNicknameProperty, 
      kABPersonFirstNamePhoneticProperty, 
      kABPersonLastNamePhoneticProperty, 
      kABPersonMiddleNamePhoneticProperty, 
      kABPersonOrganizationProperty, 
      kABPersonJobTitleProperty, 
      kABPersonDepartmentProperty, 
      kABPersonEmailProperty, 
      kABPersonBirthdayProperty, 
      kABPersonNoteProperty, 
      kABPersonCreationDateProperty, 
      kABPersonModificationDateProperty, 

      kABPersonAddressProperty, 
      kABPersonDateProperty, 
      kABPersonKindProperty, 
      kABPersonPhoneProperty, 
      kABPersonInstantMessageProperty, 
      kABPersonSocialProfileProperty, 
      kABPersonURLProperty, 
      kABPersonRelatedNamesProperty 
     }; 
     int propertyArraySize = 25; 



     for (int propertyIndex = 0; propertyIndex < propertyArraySize; propertyIndex++) { 
...code here 
}     
Cuestiones relacionadas