gpt4 book ai didi

swift - 向 MKAnnotation 添加属性会出现 EXC_BAD_EXCESS 错误

转载 作者:行者123 更新时间:2023-11-30 14:09:54 24 4
gpt4 key购买 nike

我正在尝试将我自己的属性添加到 MKAnnotation,但我这样做时出现错误。我想要完成的是除了 titlesubtitle 之外添加例如 heightrate

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

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

这就是我定义标记的方式:

    var request = NSURLRequest(URL: url!)
var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
if data != nil {
var dataArray = JSON(data: data!)

for (key, data) in dataArray {
var title = data["name"]
var subtitle = data["description"]
var lat = data["lat"]
var lng = data["lng"]

var marker = CustomPointAnnotation()
marker.coordinate = CLLocationCoordinate2D(latitude: lat.doubleValue, longitude: lng.doubleValue)
marker.title = title.string
marker.subtitle = subtitle.string
marker.imageName = "marker.png"
marker.image = "Test";
mapView.addAnnotation(marker)
}
}

这是我尝试检索数据的地方:

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
println("Called")

performSegueWithIdentifier("mapToCity", sender: self)
var pinData = view.annotation as! MapPin //EXC_BAD_ACCESS Error
var title = pinData.title
var subtitle = pinData.subtitle
var lat = pinData.coordinate.latitude
var lng = pinData.coordinate.longitude
var image = pinData.imageName
NSUserDefaults.standardUserDefaults().setObject(title, forKey: "title")
NSUserDefaults.standardUserDefaults().setObject(subtitle, forKey: "subtitle")
NSUserDefaults.standardUserDefaults().setObject(lat, forKey: "lat")
NSUserDefaults.standardUserDefaults().setObject(lng, forKey: "lng")
}

当我尝试执行var pinData行时,我收到错误Thread 1: EXC_BAD_ACCESS (code =1, address=0x4)。这可能是什么原因以及如何解决?

最佳答案

首先,您需要检查您获得的注释类型:

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
println("Called")

// Below condition is for custom annotation
let MapPin= view.annotation as? MapPin
println("Subtitle \(MapPin.subtitle)")
performSegueWithIdentifier("mapToCity", sender: self)
var title = MapPin.title
var subtitle = MapPin.subtitle
var lat = MapPin.coordinate.latitude
var lng = MapPin.coordinate.longitude
var image = MapPin.imageName
NSUserDefaults.standardUserDefaults().setObject(title, forKey: "title")
NSUserDefaults.standardUserDefaults().setObject(subtitle, forKey: "subtitle")
NSUserDefaults.standardUserDefaults().setObject(lat, forKey: "lat")
NSUserDefaults.standardUserDefaults().setObject(lng, forKey: "lng")
}else{

return nil
}
}

关于swift - 向 MKAnnotation 添加属性会出现 EXC_BAD_EXCESS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31873709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com