我在Swift的早期版本中编写了以下类。 Swift 2 编译器警告
iOS 9.0 中弃用' kABPersonAddressStreetKey ':使用 CNPostalAddress.street
并发出错误
'找不到接受的“MKPlacemark”类型的初始值设定项 类型的参数列表'(坐标: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
}
}
答案 0 :(得分:35)
将import AddressBook
替换为import Contacts
,并将String(kABPersonAddressStreetKey)
替换为String(CNPostalAddressStreetKey)
import Foundation
import MapKit
import Contacts
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(CNPostalAddressStreetKey): self.subtitle!]
let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDictionary)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = title
return mapItem
}
答案 1 :(得分:2)
您应该使用:
import Contacts
代替import AddressBook
。CNPostalAddressStreetKey
代替kABPersonAddressStreetKey
。答案 2 :(得分:2)
在此提起雷达。今天得到了这样的答复:
Engineering已提供有关此问题的以下信息: 请注意,您应该继续使用已弃用的密钥。
答案 3 :(得分:1)
您需要将副标题转换为AnyObject,如下所示:
让addressDict = [String(kABPersonAddressStreetKey):self.subtitle as! AnyObject]
您的“func mapItem() - > MKMapItem {}”的完整代码将是:
func mapItem() -> MKMapItem {
let addressDict = [String(kABPersonAddressStreetKey): self.subtitle as! AnyObject]
let placemark = MKPlacemark(coordinate: self.coordinate, addressDictionary: addressDict)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = self.title
return mapItem
}