在iOS 9.0中已经不推荐使用’kABPersonAddressStreetKey’:使用CNPostalAddress.street

前端之家收集整理的这篇文章主要介绍了在iOS 9.0中已经不推荐使用’kABPersonAddressStreetKey’:使用CNPostalAddress.street前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个早期版本的 Swift写下面的类. Swift 2编译器警告

在iOS 9.0中已经不推荐使用’kABPersonAddressStreetKey’:使用CNPostalAddress.street

并给出错误

‘Cannot find an initializer for type ‘MKPlacemark’ that accepts an
argument list of type ‘(coordinate: CLLocationCoordinate2D,
addressDictionary: [String : String?])’

我意识到可选的是解决错误,但是我尝试的似乎并不能解决问题.这是因为我是一个新手快速,任何帮助将不胜感激.

  1. import Foundation
  2. import MapKit
  3. import AddressBook
  4.  
  5. class Artwork: NSObject,MKAnnotation {
  6. let title: String?
  7. let locationName: String
  8. let discipline: String
  9. let coordinate: CLLocationCoordinate2D
  10.  
  11. init(title: String,locationName: String,discipline: String,coordinate: CLLocationCoordinate2D) {
  12. self.title = title
  13. self.locationName = locationName
  14. self.discipline = discipline
  15. self.coordinate = coordinate
  16.  
  17. super.init()
  18. }
  19.  
  20. var subtitle: String? {
  21. return locationName
  22. }
  23.  
  24. // annotation callout info button opens this mapItem in Maps app
  25. func mapItem() -> MKMapItem {
  26. let addressDictionary = [String(kABPersonAddressStreetKey): subtitle]
  27. let placemark = MKPlacemark(coordinate: coordinate,addressDictionary: addressDictionary)
  28.  
  29. let mapItem = MKMapItem(placemark: placemark)
  30. mapItem.name = title
  31.  
  32. return mapItem
  33. }
  34. }

解决方法

用导入联系人替换导入AddressBook,并用String(CNPostalAddressStreetKey)替换String(kABPersonAddressStreetKey)
  1. import Foundation
  2. import MapKit
  3. import Contacts
  4.  
  5. class Artwork: NSObject,coordinate: CLLocationCoordinate2D) {
  6. self.title = title
  7. self.locationName = locationName
  8. self.discipline = discipline
  9. self.coordinate = coordinate
  10.  
  11. super.init()
  12. }
  13.  
  14. var subtitle: String? {
  15. return locationName
  16. }
  17.  
  18. // annotation callout info button opens this mapItem in Maps app
  19. func mapItem() -> MKMapItem {
  20. let addressDictionary = [String(CNPostalAddressStreetKey): self.subtitle!]
  21. let placemark = MKPlacemark(coordinate: coordinate,addressDictionary: addressDictionary)
  22. let mapItem = MKMapItem(placemark: placemark)
  23. mapItem.name = title
  24.  
  25. return mapItem
  26.  
  27. }

猜你在找的iOS相关文章