Objective c 如何为MKPlacemark创建addressDictionary?
我尝试创建字典用于上面的代码,但没有任何效果:(Objective c 如何为MKPlacemark创建addressDictionary?,objective-c,ios5,mkannotation,addressbook,Objective C,Ios5,Mkannotation,Addressbook,我尝试创建字典用于上面的代码,但没有任何效果:( 为MKPlacemark创建addressDictionary时,建议您使用ABPerson中定义的“Address Property”常量。注意,由于这些常量属于CFStringRef类型,因此您需要将它们强制转换为(NSString*),以便将它们用作NSDictionary中的键 NSDictionary *addressDict = [[NSDictionary alloc] initWithObjectsAndKeys:
为MKPlacemark创建addressDictionary时,建议您使用ABPerson中定义的“Address Property”常量。注意,由于这些常量属于CFStringRef类型,因此您需要将它们强制转换为(NSString*),以便将它们用作NSDictionary中的键
NSDictionary *addressDict = [[NSDictionary alloc] initWithObjectsAndKeys:
location.countryCode, @"CountryCode",
location.country,@"kABPersonAddressCountryKey",
location.state, kABPersonAddressStateKey,
location.city, @"City",
location.street, kABPersonAddressStreetKey,
location.zip, kABPersonAddressZIPKey,
nil];
iOS 9+更新:使用新联系人框架
NSDictionary *addressDict = @{
(NSString *) kABPersonAddressStreetKey : location.street,
(NSString *) kABPersonAddressCityKey : location.city,
(NSString *) kABPersonAddressStateKey : location.state,
(NSString *) kABPersonAddressZIPKey : location.zip,
(NSString *) kABPersonAddressCountryKey : location.country,
(NSString *) kABPersonAddressCountryCodeKey : location.countryCode
};
值得注意的是,您需要将“AddressBook.framework”添加到项目生成设置中。 同时在头文件(.h文件)中导入:
国家/地区密钥在引号中,但不应该在引号中。请参阅。我只是显示了不同的变量,它们都不起作用。您可以详细描述一下它是如何起作用的吗?例如,如果它崩溃了,错误消息是什么?如果不崩溃,具体是什么?在init?MKplacemark工作后,您如何处理placemark变量?MKplacemark工作,但在我点击时不显示地址它。除了一个要测试的地址行之外,我对地址行进行了注释,现在它可以工作了。奇怪。正确的语法是:“location.street,kABPersonAddressStreetKey”,如果你使用ARC,你需要在cast中添加
\u bridge
,比如:(\u bridge NSString*)kABPersonAddressStreetKey:location.street
别忘了导入(=9.x)尽管新的联系人框架在iOS 9中可用,但某些使用它们的API仅在iOS 10中可用。(例如,MKPlacement initWithCoordinate:PostLaddress仅在iOS 10中可用),因此您必须使用AbAddress方法:(
NSDictionary *addressDict = @{
(NSString *) kABPersonAddressStreetKey : location.street,
(NSString *) kABPersonAddressCityKey : location.city,
(NSString *) kABPersonAddressStateKey : location.state,
(NSString *) kABPersonAddressZIPKey : location.zip,
(NSString *) kABPersonAddressCountryKey : location.country,
(NSString *) kABPersonAddressCountryCodeKey : location.countryCode
};
NSDictionary *addressDict = @{
CNPostalAddressStreetKey : location.street,
CNPostalAddressCityKey : location.city,
CNPostalAddressStateKey : location.state,
CNPostalAddressPostalCodeKey : location.zip,
CNPostalAddressCountryKey : location.country,
CNPostalAddressISOCountryCodeKey : location.countryCode
};
#import <AddressBook/AddressBook.h>
(NSString*)kABPersonAddressStreetKey
(NSString*)kABPersonAddressCityKey
(NSString*)kABPersonAddressStateKey
(NSString*)kABPersonAddressCountryKey