2015-06-27 7 views
14

Mam następującą klasę napisaną we wcześniejszej wersji Swift. Swift 2 kompilator ostrzega, że ​​'KABPersonAddressStreetKey' był nieaktualny w iOS 9.0: użyj CNPostalAddress.street

'kABPersonAddressStreetKey' została zaniechana w iOS 9.0: używać CNPostalAddress.street

i daje błąd

„nie można znaleźć inicjatora dla typu "MKPlacemark", który akceptuje listę argumentów typu " " (współrzędna: CLLocationCoordinate2D, addressDic c: [String: String?]) "

Zdaję sobie sprawę, że potrzebne są opcje, aby rozwiązać ten problem, ale cokolwiek próbuję, nie rozwiązuje problemu. Wynika to z tego, że jestem początkującym szybkim i każda pomoc będzie doceniona.

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

Odpowiedz

34

Wymień import AddressBook z import Contacts a także wymienić String(kABPersonAddressStreetKey) z 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

Należy użyć:

  1. import Contacts zamiast import AddressBook.
  2. CNPostalAddressStreetKey zamiast tego kABPersonAddressStreetKey.
+1

To jest stare pytanie z już bardzo upragnioną odpowiedzią. Jak twoja odpowiedź różni się od zaakceptowanej odpowiedzi? – JAL

1

Trzeba oddać napisów jako AnyObject jak pokazano poniżej:

niech addressDict = [String (kABPersonAddressStreetKey): self.subtitle jak! AnyObject]

i kompletnym kod dla "func mapItem() -> MKMapItem {}" będzie:

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

Zapisano radar na ten temat. Otrzymałem tę odpowiedź dzisiaj:

Inżynier podał następujące informacje dotyczące tego problemu: Należy pamiętać, że należy nadal używać nieaktualnych kluczy.

Powiązane problemy