【问题标题】:'kABPersonAddressStreetKey' was deprecated in iOS 9.0: use CNPostalAddress.street'kABPersonAddressStreetKey' 在 iOS 9.0 中已弃用:使用 CNPostalAddress.street
【发布时间】:2015-06-27 04:55:20
【问题描述】:

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

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

然后报错

'找不到接受 类型的参数列表'(坐标:CLLocationCoordinate2D, 地址字典:[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
  }
}

【问题讨论】:

    标签: ios swift2


    【解决方案1】:

    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
    
    }
    

    【讨论】:

      【解决方案2】:

      你应该使用:

      1. import Contacts 而不是 import AddressBook
      2. CNPostalAddressStreetKey 而不是 kABPersonAddressStreetKey

      【讨论】:

      • 这是一个老问题,答案已经得到高度评价。您的答案与接受的答案有何不同?
      • 这个答案为objective-c增加了价值。 CNPostalAddressStreetKey 是 swift 的 CNPostalAddress.street 的 Objective-c 版本
      【解决方案3】:

      为此提交了雷达。今天收到了这个回复:

      Engineering 提供了有关此问题的以下信息: 请注意,您应该继续使用已弃用的密钥。

      【讨论】:

        【解决方案4】:

        您需要将您的字幕转换为 AnyObject,如下所示:

        让 addressDict = [String(kABPersonAddressStreetKey): self.subtitle as!任何对象]

        您的“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
          }
        

        【讨论】:

          猜你喜欢
          • 2015-09-15
          • 2016-01-14
          • 1970-01-01
          • 1970-01-01
          • 2021-01-31
          • 1970-01-01
          • 2016-01-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多