Andrew
Andrew

Reputation: 8563

How to use Key Value Coding to Set Property Values in ABPerson

Given this key/value pair:
FirstName = "Steve"

I'd like to set the property of an ABPerson in the iPhone Address Book, like so: ABRecordSetValue(person, kABPersonFirstNameProperty, @"Steve", nil);

The problem is, I won't always know what key is submitted. (It will be equivalent to a property constant without the "kABPerson" before it, and "Property" after it). The submitted key could be any number of properties: "JobTitle", "Note", etc. How do I translate that key into the Address Book Property Constant. ie:
FirstName to kABPersonFirstNameProperty.

I was thinking I could use Key Value Coding, but I'm at a loss as to how I'd implement it in this case.

For the sake of coding efficiency, I'm trying to avoid using a long Switch statement, or using an array filled with every possible Property.

Update

I'm discovering here that I need to access a constant by it's string representation. But I don't know if that's possible. So kABPersonJobTitleProperty is a constant that equals 18. So I would love if the following code worked:

  NSString *fieldName = [NSString stringWithString:@"JobTitle"];
  NSString *propertyName = [NSString stringWithFormat:@"kABPerson%@Property", fieldName];

ABRecordRef person = ABPersonCreate(); ABRecordSetValue(person, propertyName, @"Fisherman", nil);

Of course, running this returns this warning passing argument 2 of 'ABRecordSetValue' makes integer from pointer without a cast. Because the string propertyName is not resolving to the integer or constant it's looking for.

Can anyone see a way to make this work?

My setup is similar to the question listed here:
Key-Value Coding

I'm getting xml data that I have control over, and turning it into properties of an Address Book entry.

Upvotes: 2

Views: 976

Answers (1)

greg
greg

Reputation: 4953

This is more of a application design question than anything else. The way I see it with what you have, you're going to need a "long switch statement" somewhere. Maybe the cleanest way would be in a message you send yourself with a signature like:

- (ABPropertyID)recordPropertyFromMyEnum:(MyEnumType)property

Consider this: if the only enumerations you have line up one-for-one with an ABPropertyID, why not just use ABPropertyID instead?

Upvotes: 2

Related Questions