 |
 |
|
 |
07-01-2009, 04:40 PM
|
#1 (permalink)
|
|
Registered Member
Join Date: May 2009
Posts: 9
|
error pulling a city from Address Book
Hi, I have some code that pulls the addresses from the AddressBook, and if there is one, it then gets the city. I only care about the city!
It is currently giving a warning that I can't seem to get to go away. The code *seems* to work fine on the simulator but does not work at all on the iPhone.
This is the code I am using:
Code:
ABMutableMultiValueRef addressMulti = ABRecordCopyValue(person, kABPersonAddressProperty);
NSMutableArray *address = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < ABMultiValueGetCount(addressMulti); i++) {
NSString *city = [(NSString*)ABMultiValueCopyValueAtIndex(addressMulti, i) autorelease];
[address addObject:city];
}
if ([address count] > 0) {
NSMutableDictionary *primaryAddress = [address objectAtIndex:0];
meCity1 = [primaryAddress objectForKey:kABPersonAddressCityKey];
************ WARNING: passing argument 1 of 'objectForKey:' from incompatible pointer type **********
NSLog(@"%@", meCity1);
}
meCity1 is set up earlier in the program
Any help with this would be greatly appreciated
Last edited by AlanTaylor; 07-01-2009 at 05:44 PM.
Reason: Ommission
|
|
|
07-01-2009, 05:30 PM
|
#2 (permalink)
|
|
Registered Member
Join Date: Sep 2008
Location: Denver
Posts: 158
|
Code:
NSString *city = [(NSString*)ABMultiValueCopyValueAtIndex(addressMulti, i) autorelease];
city is an NSString which you've added to the mutable array address.
Later on, you do the following
Code:
NSMutableDictionary *primaryAddress = [address objectAtIndex:0];
The object at index 0 is a string, why are you trying to define a string as a mutable dictionary.
|
|
|
07-01-2009, 05:51 PM
|
#3 (permalink)
|
|
Registered Member
Join Date: May 2009
Posts: 9
|
Quote:
Originally Posted by BSDimwit
Code:
NSString *city = [(NSString*)ABMultiValueCopyValueAtIndex(addressMulti, i) autorelease];
city is an NSString which you've added to the mutable array address.
Later on, you do the following
Code:
NSMutableDictionary *primaryAddress = [address objectAtIndex:0];
The object at index 0 is a string, why are you trying to define a string as a mutable dictionary.
|
I see. I have tried changing city to be an NSMutableDictionary instead, but I get the same error. Am I missing the point here?
|
|
|
07-01-2009, 06:00 PM
|
#4 (permalink)
|
|
Registered Member
Join Date: Sep 2008
Location: Denver
Posts: 158
|
Yes you are.
You are missing the point. You are trying to get a list of cities from the address book right?
You are already doing that by in the first section of your code here...
Code:
ABMutableMultiValueRef addressMulti = ABRecordCopyValue(person, kABPersonAddressProperty);
NSMutableArray *address = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < ABMultiValueGetCount(addressMulti); i++) {
NSString *city = [(NSString*)ABMultiValueCopyValueAtIndex(addressMulti, i) autorelease];
[address addObject:city];
}
Why are you doing the other stuff below that. Your address array already has the cities in it? Perhaps you aren't being specific enough as to what you are trying to accomplish. The first error I pointed is that you trying to turn a string into a dictionary...which won't work.
|
|
|
07-01-2009, 06:07 PM
|
#5 (permalink)
|
|
Registered Member
Join Date: May 2009
Posts: 9
|
Quote:
Originally Posted by BSDimwit
You are missing the point. You are trying to get a list of cities from the address book right?
You are already doing that by in the first section of your code here...
Code:
ABMutableMultiValueRef addressMulti = ABRecordCopyValue(person, kABPersonAddressProperty);
NSMutableArray *address = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < ABMultiValueGetCount(addressMulti); i++) {
NSString *city = [(NSString*)ABMultiValueCopyValueAtIndex(addressMulti, i) autorelease];
[address addObject:city];
}
Why are you doing the other stuff below that. Your address array already has the cities in it? Perhaps you aren't being specific enough as to what you are trying to accomplish. The first error I pointed is that you trying to turn a string into a dictionary...which won't work.
|
The section of code you just quoted is actually giving me the entire address - I just want to extract the city from that.
The rest of the code checks first to see if there is an address for that person, and if there is I want it to then extract the city from that with the kABPersonAddressCityKey.
Everything else I have done with the addressbook has been single items such as names and images, this is the first time I have tried to take a value from something that has several.
EDIT: changing city to be NSMutableDictionary still gives an error - but seems to actually be working on both the Simulator and the phone...
|
|
|
07-01-2009, 06:14 PM
|
#6 (permalink)
|
|
Registered Member
Join Date: Sep 2008
Location: Denver
Posts: 158
|
Here is my code
Here is my code for plucking useful bits out of the addressBook, see if you can use any of it. This code works on both the sim and phone.
Code:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
customer.firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
customer.lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
customer.companyName = (NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);
if (!customer.firstName.length) {
customer.firstName = @"*";
}
if (!customer.lastName.length) {
customer.lastName = @"*";
}
if (!customer.companyName.length) {
customer.companyName = @"";
}
CFStringRef email, emailLabel, phone, phoneLabel;
ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSMutableDictionary *myPhoneDict = [NSMutableDictionary dictionaryWithCapacity:ABMultiValueGetCount(phoneMulti)];
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneMulti); i++) {
phoneLabel = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phoneMulti, i));
phone = ABMultiValueCopyValueAtIndex(phoneMulti, i);
[myPhoneDict setObject:(NSString*)phone forKey:(NSString*)phoneLabel];
CFRelease(phone);
CFRelease(phoneLabel);
}
if ( [myPhoneDict objectForKey:@"mobile"] != nil) {
customer.phone = [myPhoneDict objectForKey:@"mobile"];
} else if ( [myPhoneDict objectForKey:@"home"] != nil) {
customer.phone = [myPhoneDict objectForKey:@"home"];
} else if ( [myPhoneDict objectForKey:@"work"] != nil) {
customer.phone = [myPhoneDict objectForKey:@"work"];
}
ABMutableMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty);
NSMutableDictionary *myEmailDict = [NSMutableDictionary dictionaryWithCapacity:ABMultiValueGetCount(multi)];
for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++) {
emailLabel = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(multi, i));
email = ABMultiValueCopyValueAtIndex(multi, i);
[myEmailDict setObject:(NSString*)email forKey:(NSString*)emailLabel];
CFRelease(email);
CFRelease(emailLabel);
}
if ([myEmailDict objectForKey:@"home"] != nil) {
customer.email = [myEmailDict objectForKey:@"home"];
} else if ([myEmailDict objectForKey:@"work"] != nil) {
customer.email = [myEmailDict objectForKey:@"work"];
} else if ([myEmailDict objectForKey:@"other"] != nil) {
customer.email = [myEmailDict objectForKey:@"other"];
}
// get the address stuff
ABMultiValueRef streets = ABRecordCopyValue(person, kABPersonAddressProperty);
NSMutableDictionary *myAddressDict = [NSMutableDictionary dictionaryWithCapacity:ABMultiValueGetCount(streets)];
for (CFIndex j = 0; j < ABMultiValueGetCount(streets); j++) {
NSMutableDictionary *myLabelDict = [[NSMutableDictionary alloc] init];
CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(streets, j);
CFStringRef typeTmp = ABMultiValueCopyLabelAtIndex(streets, j);
CFStringRef type = ABAddressBookCopyLocalizedLabel(typeTmp);
NSString *street = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStreetKey) copy];
NSString *city = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCityKey) copy];
NSString *state = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStateKey) copy];
NSString *zip = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressZIPKey) copy];
NSString *country = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCountryKey) copy];
if ((street != nil) && (street.length > 0))
[myLabelDict setObject:street forKey:@"street"];
if ((city != nil) && (city.length > 0))
[myLabelDict setObject:city forKey:@"city"];
if ((state != nil) && (state.length > 0))
[myLabelDict setObject:state forKey:@"state"];
if ((zip != nil) && (zip.length > 0))
[myLabelDict setObject:zip forKey:@"zip"];
if ((country != nil) && (country.length > 0))
[myLabelDict setObject:country forKey:@"country"];
[myAddressDict setObject:myLabelDict forKey:(NSString *)type];
[myLabelDict release];
[street release];
[city release];
[state release];
[zip release];
[country release];
CFRelease(dict);
CFRelease(type);
CFRelease(typeTmp);
}
NSString *theKey;
if ([myAddressDict objectForKey:@"home"] != nil) {
theKey = @"home";
} else if ([myAddressDict objectForKey:@"work"] != nil) {
theKey = @"work";
} else if ([myAddressDict objectForKey:@"other"] != nil) {
theKey = @"other";
}
if ([[[myAddressDict objectForKey:theKey] objectForKey:@"street"] length] > 0) {
customer.street = [[myAddressDict objectForKey:theKey] objectForKey:@"street"];
} else {
customer.street = @"";
}
if ([[[myAddressDict objectForKey:theKey] objectForKey:@"city"] length] > 0) {
customer.city = [[myAddressDict objectForKey:theKey] objectForKey:@"city"];
} else {
customer.city = @"";
}
if ([[[myAddressDict objectForKey:theKey] objectForKey:@"state"] length] > 0) {
customer.state = [[myAddressDict objectForKey:theKey] objectForKey:@"state"];
} else {
customer.state = @"";
}
if ([[[myAddressDict objectForKey:theKey] objectForKey:@"zip"] length] > 0) {
customer.postalCode = [[myAddressDict objectForKey:theKey] objectForKey:@"zip"];
} else {
customer.postalCode = @"";
}
if ([[[myAddressDict objectForKey:theKey] objectForKey:@"country"] length] > 0) {
customer.country = [[myAddressDict objectForKey:theKey] objectForKey:@"country"];
} else {
customer.country = @"";
}
CFRelease(streets);
CFRelease(phoneMulti);
CFRelease(multi);
[customer dehydrate];
[self dismissModalViewControllerAnimated:YES];
return NO;
}
|
|
|
07-02-2009, 04:17 AM
|
#7 (permalink)
|
|
Pro. Game Developer
iPhone Dev SDK Supporter
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 1,261
|
Quote:
Originally Posted by AlanTaylor
Hi, I have some code that pulls the addresses from the AddressBook, and if there is one, it then gets the city. I only care about the city!
It is currently giving a warning that I can't seem to get to go away. The code *seems* to work fine on the simulator but does not work at all on the iPhone.
This is the code I am using:
Code:
ABMutableMultiValueRef addressMulti = ABRecordCopyValue(person, kABPersonAddressProperty);
NSMutableArray *address = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < ABMultiValueGetCount(addressMulti); i++) {
NSString *city = [(NSString*)ABMultiValueCopyValueAtIndex(addressMulti, i) autorelease];
[address addObject:city];
}
if ([address count] > 0) {
NSMutableDictionary *primaryAddress = [address objectAtIndex:0];
meCity1 = [primaryAddress objectForKey:kABPersonAddressCityKey];
************ WARNING: passing argument 1 of 'objectForKey:' from incompatible pointer type **********
NSLog(@"%@", meCity1);
}
meCity1 is set up earlier in the program
Any help with this would be greatly appreciated
|
The compiler is being very specific in its warning. It doesn't like the argument being passed as the first parameter. So.... how is 'kABPersonAddressCityKey' defined?
__________________
Code free of compiler warnings and errors, and code that won't crash are not mutually exclusive.
How your application performs in the simulator is irrelevant, unless your target audience is people who will run your app only on the simulator.
|
|
|
07-02-2009, 09:48 AM
|
#8 (permalink)
|
|
Registered Member
Join Date: May 2009
Posts: 9
|
Quote:
Originally Posted by Kalimba
The compiler is being very specific in its warning. It doesn't like the argument being passed as the first parameter. So.... how is 'kABPersonAddressCityKey' defined?
|
I'm guessing my code isn't setting it at all if it is just making a really long string, but I'm confused as to why this happens when the values are copied into a dictionary. This is all making me feel very stupid >.<
BSDimwit - your code worked perfectly, thank you very much for sharing it with me!
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 486 |
| 47 members and 439 guests |
| Abdel, ackpth, activ8, AjohnB, Alchemda, BostonMerlin, BrianSlick, Centurion Games, ChrisMayer, CoolApps, Corey, dbarrett, dda, designomatt, dmf1978, eatenbyrats, Gamer211, gonk, greenuns, Gudus, imsatasia, IphoneSdk, jharrah, Kalimba, Knertified, LemonMeringue, Link, MiniRobinho, msudan, naomipunkclan, NeilB, nibby, Noise, P2k, pashik, ryguy2503, simpsonaty, Snappy, StefanL, svguerin3, tcarte04, the1nz4ne, treazer, TunaNugget, Tuszy, victorsk, ZunePod |
| Most users ever online was 779, 05-11-2009 at 10:55 AM. |
» Stats |
Members: 21,510
Threads: 35,793
Posts: 156,804
Top Poster: smasher (2,449)
|
| Welcome to our newest member, dmf1978 |
|