Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create CNPostalAddress from known values

I am trying to create a CNPostalAddress with some strings in Objective-C. I have street address, city, state, zip and country values. I have tried the code below but it’s not working. Thanks for your valuable time.

CNPostalAddress *postalAddr = [[CNPostalAddress alloc] init];
postalAddr.street = [NSString stringWithFormat:@"%@ %@",street1,street2];// here, I am getting an error: Street property is read only.
like image 963
SRI Avatar asked Apr 15 '16 06:04

SRI


2 Answers

Create a CNMutablePostalAddress instead of a CNPostalAddress:

CNMutablePostalAddress *postalAddr = [[CNMutablePostalAddress alloc] init];
postalAddr.street = [NSString stringWithFormat:@"%@ %@", street1, street2];

CNMutablePostalAddress is a subclass of CNPostalAddress, so you can use it like a CNPostalAddress from this point onward.

like image 88
Minh Nguyễn Avatar answered Nov 13 '22 15:11

Minh Nguyễn


The above answer rewritten in Swift:

let postalAddr = CNMutablePostalAddress()
postalAddr.street = String(format: "%@ %@", street1, street2)
like image 6
Arek Avatar answered Nov 13 '22 16:11

Arek