Re: Ignoring an empty field in the address book (bug?)
Re: Ignoring an empty field in the address book (bug?)
- Subject: Re: Ignoring an empty field in the address book (bug?)
- From: David Martin <email@hidden>
- Date: Mon, 6 Jan 2003 07:48:50 +0100
On Monday, Jan 6, 2003, at 06:24 Europe/Paris, Ben Mackin wrote:
Well, it looks like most of us are stumped. From the bit of offlist
help I got, this one onlist message, and some more messing around on
my part, I have come to the conclusion it is not possible to check if
a property exists for a given ABPerson (for example checking if the
given person has a company associated with them or not).
Ben,
I'm sorry, but what I do in Address Book Exporter
<
http://www.gwenhiver.net/abe> is basically what has already been
suggested testing against nil.
Somewhere in my code, I have a method called:
- (NSString *)exportUsingCustomSettings:(NSEnumerator *)peopleEnumerator
which is given [[[ABAddressBook sharedAddressBook] people]
objectEnumerator], for example.
Inside this method, I have something like:
ABRecord *aRecord;
while (aRecord = [peopleEnumerator nextObject]) {
NSMutableArray *exportedRecord = [NSMutableArray array];
if ([defaults boolForKey:ABEExportFirstName]) {
[exportedRecord addObject:[self
stringWithLabel:kABFirstNameProperty fromRecord:aRecord]]; }
}
-stringWithLabel:fromRecord:, in turn, looks like:
- (NSString *)stringWithLabel:(NSString *)label fromRecord:(ABRecord
*)record {
return [self stringFromProperty:[record valueForProperty:label]];
}
And at last, -stringFromProperty: is basically:
- (NSString *)stringFromProperty:(NSString *)property {
// Each time we want to add a property value, we have to ensure it
is not nil. We also want to remove any linefeeds.
NSMutableString *string;
if (property) {
// We have to replace a few characters that will be confusing
for the program trying to reimport the files.
string = [NSMutableString stringWithString:property];
[string replaceOccurrencesOfString:@"\n" withString:@" "
options:0 range:NSMakeRange(0, [string length])];
[string replaceOccurrencesOfString:@"\r" withString:@" "
options:0 range:NSMakeRange(0, [string length])];
[string replaceOccurrencesOfString:@"\"" withString:@" "
options:0 range:NSMakeRange(0, [string length])];
[string replaceOccurrencesOfString:@"\t" withString:@" "
options:0 range:NSMakeRange(0, [string length])];
} else {
string = [NSMutableString string];
}
return string;
}
I never got any problem with this implementation, and I can assure you
I export fields that are not even supported by Address Book (though
they are by AddressBook.framework) and so are very likely to be empty.
Hope this helps,
David
So what I wonder is how others get around this. How do you list all
the names in the address book in your programs for example? My address
book has some entries with no last names, and my code just craps out
at that point (once it tries to grab the last name on a record that
doesn't have one, it quits out). And I can't check if the record has a
last name or not, because it just raises an exception. You would think
it should just return n empty string...
Try changing that if statement to:
if ([[peopleFound objectAtIndex:i]
valueForProperty:kABLastNameProperty])
Unfortunatly same thing. It stops and says:
[NSCFString stringByAppendingString:]: nil string (or other) argument
It is never making it past [peopleFound objectAtIndex:i]
valueForProperty:kABLastNameProperty]. If I just do the following:
NSLog([peopleFound objectAtIndex:i]
valueForProperty:kABLastNameProperty])
It wont do that line (I would have thought it would display an empty
string at least), and reports:
[NSCFString stringByAppendingString:]: nil string (or other) argument
So it is something about checking none existent properties I guess...
Thanks,
Ben
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.