我有一个早期版本的
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?])’
我意识到可选的是解决错误,但是我尝试的似乎并不能解决问题.这是因为我是一个新手快速,任何帮助将不胜感激.
import Foundation import MapKit import AddressBook class Artwork: NSObject,MKAnnotation { let title: String? let locationName: String let discipline: String let coordinate: CLLocationCoordinate2D init(title: String,locationName: String,discipline: String,coordinate: CLLocationCoordinate2D) { self.title = title self.locationName = locationName self.discipline = discipline self.coordinate = coordinate super.init() } var subtitle: String? { return locationName } // annotation callout info button opens this mapItem in Maps app func mapItem() -> MKMapItem { let addressDictionary = [String(kABPersonAddressStreetKey): subtitle] let placemark = MKPlacemark(coordinate: coordinate,addressDictionary: addressDictionary) let mapItem = MKMapItem(placemark: placemark) mapItem.name = title return mapItem } }
解决方法
用导入联系人替换导入AddressBook,并用String(CNPostalAddressStreetKey)替换String(kABPersonAddressStreetKey)
import Foundation import MapKit import Contacts class Artwork: NSObject,coordinate: CLLocationCoordinate2D) { self.title = title self.locationName = locationName self.discipline = discipline self.coordinate = coordinate super.init() } var subtitle: String? { return locationName } // annotation callout info button opens this mapItem in Maps app func mapItem() -> MKMapItem { let addressDictionary = [String(CNPostalAddressStreetKey): self.subtitle!] let placemark = MKPlacemark(coordinate: coordinate,addressDictionary: addressDictionary) let mapItem = MKMapItem(placemark: placemark) mapItem.name = title return mapItem }