【问题标题】:Saving a CNPostalAddress in Objective-C在 Objective-C 中保存 CNPostalAddress
【发布时间】:2016-05-20 06:35:21
【问题描述】:

我有这个代码:

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.postalAddresses”中,但我需要先进行转换,否则应用程序将崩溃并出现异常。 @意义问题

标签: ios objective-c contacts cnpostaladdress


【解决方案1】:

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

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

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

【讨论】:

  • 是的!非常感谢! :) @meaning-matters
  • @David 很高兴为您提供帮助!你也可以投票^这个答案,以防你不知道;-)
【解决方案2】:

这是正确的实现。

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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多