David David - 4 months ago 75
iOS Question

OBJECTIVE-C: Saving a CNPostalAddress

I have this code:

CNMutablePostalAddress *postalAddress = [[CNMutablePostalAddress alloc] init];
[postalAddress setCity:@"City"];

CNLabeledValue *city = [CNLabeledValue labeledValueWithLabel:CNPostalAddressCityKey value:postalAddress.city];

NSArray<CNLabeledValue<CNMutablePostalAddress *> *> *postalAddresses = @[city];

contact.postalAddresses = @[postalAddresses];


I don't know how I can do this conversion, because I need to pass to contact.postalAddress an Array like this in the code. I have tried everything possible but nothing.

This code give me this exception:

*** Terminating app due to uncaught exception 'CNPropertyInvalidTypeException', reason: 'Labeled value (
"<CNLabeledValue: 0x12d5a14c0: identifier=080A5D1B-1F2D-4EE2-AB6F-BFAD523DA1C9, label=city, value=City>") has incorrect type __NSArrayI for key postalAddresses. It should be CNLabeledValue.'


How could I do this?

Answer

This creates a new postal address and adds it to the contact. This is what you want, right?

CNMutablePostalAddress *postalAddress = [[CNMutablePostalAddress alloc] init];
address.city = @"City";

CNLabeledValue *labeledValue = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:address];
contact.postalAddresses = @[labeledValue];
Comments