Alterplay / APAddressBook

Easy access to iOS address book
MIT License
1.38k stars 193 forks source link

How can I get firstName and add in a Dictionary? #99

Closed eduardobonfa closed 9 years ago

eduardobonfa commented 9 years ago

HI, I trying to add firstName and phone number in a dictionary so I can deal with it later. So I did this

NSDictionary *contactDictionary = @{ @"firstName" : contact.firstName, @"middleName" : contact.middleName, @"lastName" : contact.lastName, @"phones" : contact.phones, }; But I get this error: Property 'firstName' not found on object of type 'APContact'. (and it's TRUE)

but When I try putting contact.name.firstName it doesn`t work either.

What can I do?

belkevich commented 9 years ago

Please, share you code of using of APAddressBook

eduardobonfa commented 9 years ago

This is my code

NSMutableArray* returnContacts;
APContact* contact;

- (void)reloadContacts {

    APAddressBook *addressBook = [[APAddressBook alloc] init];

    addressBook.fieldsMask = APContactFieldDefault;
    addressBook.sortDescriptors = @[
                                    [NSSortDescriptor sortDescriptorWithKey:@"name.firstName" ascending:YES],
                                    [NSSortDescriptor sortDescriptorWithKey:@"name.lastName" ascending:YES]
                                    ];
    addressBook.filterBlock = ^BOOL(APContact *contact)
    {
        return contact.phones.count > 0;
    };
    [addressBook loadContacts:^(NSArray <APContact *> *contacts, NSError *error) {
        if (!error) {
            for (int i = 0; i < contacts.count; i++){
                contact = [contacts objectAtIndex:i];
                NSDictionary *contactDictionary = @{
                                     @"firstName" : contact.firstName,
                                     @"middleName" : contact.middleName,
                                     @"lastName" : contact.lastName,
                                     @"phones" : contact.phones,
};
            }
            [returnContacts addObject:contactDictionary];
        } else if (error) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
                                                                message:error.localizedDescription
                                                               delegate:nil
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:nil];
            [alertView show];
        }
    }];
}
belkevich commented 9 years ago

Looks like problem is in

@"firstName" : contact.firstName,
@"middleName" : contact.middleName,
@"lastName" : contact.lastName,
@"phones" : contact.phones,

there are no such properties, except phones. Use contact.name.firstName etc.

eduardobonfa commented 9 years ago

if I try it, I get this error

Property 'firstName' cannot be found in forward class object 'APName'

belkevich commented 9 years ago

Add #import <APAddressBook/APName.h>?

eduardobonfa commented 9 years ago

it worked, thank you, sorry I'm new at objective c

belkevich commented 9 years ago

It's ok. Have a nice day!