Creating CNPostalAddress from Known Values

I am trying to create a CNPostalAddress with some lines in Objective-C. I have an address, city, state, zip code and country. I tried the code below, but it does not work. Thank you for your precious time.

 CNPostalAddress *postalAddr = [[CNPostalAddress alloc] init]; postalAddr.street = [NSString stringWithFormat:@"%@ %@",street1,street2];// here, I am getting an error: Street property is read only. 
+13
ios objective-c ios9 cnpostaladdress
source share
2 answers

Create CNMutablePostalAddress instead of CNPostalAddress:

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

CNMutablePostalAddress is a subclass of CNPostalAddress, so you can use it as CNPostalAddress from now on.

+22
source share

The above answer is rewritten in Swift:

 let postalAddr = CNMutablePostalAddress() postalAddr.street = String(format: "%@ %@", street1, street2) 
0
source share

All Articles