在 Objective-C 中保存 CNPostalAddress

Saving a CNPostalAddress in Objective-C

我有这个代码:

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

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

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

contact.postalAddresses = @[postalAddresses];

我不知道如何进行这种转换,因为我需要在代码中向 contact.postalAddress 传递一个数组。我已经尝试了一切可能,但一无所获。

这段代码给我这个例外:

*** 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.'

我该怎么做?

这将创建一个新的邮政地址并将其添加到 contact。这就是你想要的,对吧?

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

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

这是正确的实现方式。

CNMutablePostalAddress *postalAddress = [[CNMutablePostalAddress alloc] init];
postalAddress.street = @"1 Market Street";
postalAddress.city = @"Sydney";
postalAddress.postalCode = @"2000";
postalAddress.state = @"NSW";
CNLabeledValue *address = [CNLabeledValue labeledValueWithLabel:CNLabelWork value:postalAddress];
contact.postalAddresses = @[address];