我正在尝试为地图添加一个简单的注释。但是它在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)
}
答案 0 :(得分:1)
MKAnnotation
协议要求属性为var
,字符串为可选(!
)。
在MyMapAnnotation
类中,更改这些声明:
let title: String
let subtitle: String
let coordinate: CLLocationCoordinate2D
到此:
var title: String!
var subtitle: String!
var coordinate: CLLocationCoordinate2D