Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'kABPersonAddressStreetKey' was deprecated in iOS 9.0: use CNPostalAddress.street

Tags:

ios

swift2

I have the following class written in an earlier version of Swift. The Swift 2 Compiler warns that

'kABPersonAddressStreetKey' was deprecated in iOS 9.0: use CNPostalAddress.street

and gives an error

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

I realise that optionals are required to resolve the error but whatever I try does not seem to resolve the problem. This is due to me being a newbie to swift and any help would be appreciated.

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
  }
}
like image 331
Paul Sumner Avatar asked Jun 27 '15 04:06

Paul Sumner


1 Answers

Replace import AddressBook with import Contacts and also replace String(kABPersonAddressStreetKey) with 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

}
like image 94
Paul Sumner Avatar answered Oct 20 '22 00:10

Paul Sumner