gpt4 book ai didi

ios - 'kABPersonAddressStreetKey' 在 iOS 9.0 : use CNPostalAddress. 街道中被弃用

转载 作者:技术小花猫 更新时间:2023-10-29 10:12:56 26 4
gpt4 key购买 nike

我用早期版本的 Swift 编写了以下类。 Swift 2 编译器警告

'kABPersonAddressStreetKey' 在 iOS 9.0 中被弃用:使用 CNPostalAddress.street

并报错

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

我意识到需要可选值来解决错误,但无论我尝试什么,似乎都无法解决问题。这是因为我是 swift 的新手,我们将不胜感激。

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
}
}

最佳答案

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

}

关于ios - 'kABPersonAddressStreetKey' 在 iOS 9.0 : use CNPostalAddress. 街道中被弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31085299/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com