/ / MKAnnotation Swift - ios, iphone, veloz

MKAnnotation Swift - ios, iphone, veloz

Não sei como anotar um mapa na linguagem rápida. Eu não sei como criar a classe NSObject. O seguinte é o código que eu tentei, mas não conseguiu executar:

import Foundation
import MapKit
class MapPin : MKAnnotation
{
var mycoordinate: CLLocationCoordinate2D
var mytitle: String
var mysubtitle: String

func initMapPin (coordinate: CLLocationCoordinate2D!, title: String!, subtitle: String!)
{
mycoordinate = coordinate
mytitle = title
mysubtitle = subtitle
}
}

Respostas:

90 para resposta № 1
  1. Todos os métodos de inicialização no Swift devem ser simplesmente "init"
  2. MKAnnotation requer que o objeto herde de NSObjectProtocol. Para fazer isso, você deve ter sua classe herdada do NSObject
  3. Você deve declarar suas propriedades para corresponder às do protocolo MKAnnotation
  4. Você não deve declarar seus parâmetros como Opcionais implicitamente não-embalados, a menos que você realmente precise. Deixe o compilador verificar se algo é nulo em vez de gerar erros de tempo de execução.

Isso te dá o resultado:

class MapPin : NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?

init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String) {
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
}
}