添加注释时Mapkit崩溃

时间:2014-07-31 09:29:22

标签: ios swift mapkit

我正在尝试为地图添加一个简单的注释。但是它在addAnnotation方法上崩溃了。怎么了?我正在使用Swift。正确设置了地图与Interface Builder中对象之间的连接。我得到的只是EXC_BAD_ACCESS

class MyMapAnnotation : NSObject, MKAnnotation {

    let title: String
    let subtitle: String
    let coordinate: CLLocationCoordinate2D

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

我在我的视图控制器中尝试这个:

let coordinate = CLLocationCoordinate2D(latitude: 46.830930, longitude: 7.705106)
let annotation = MyMapAnnotation(title: "Title", subtitle: "Subtitle", coordinate: coordinate)

if CLLocationCoordinate2DIsValid(coordinate) {
    map.addAnnotation(annotation)
}

1 个答案:

答案 0 :(得分:1)

MKAnnotation协议要求属性为var,字符串为可选(!)。


MyMapAnnotation类中,更改这些声明:

let title: String
let subtitle: String
let coordinate: CLLocationCoordinate2D

到此:

var title: String!
var subtitle: String!
var coordinate: CLLocationCoordinate2D