gpt4 book ai didi

swift - 自定义 MKAnnotation 字典输入 (CLLocationCooperative2d)

转载 作者:行者123 更新时间:2023-11-30 13:27:20 25 4
gpt4 key购买 nike

import MapKit
import UIKit

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



init(id: String, dictionary: Dictionary<String, AnyObject>){


self.id = id

if let title = dictionary["title"] as? String {
self.title = title
}

if let subtitle = dictionary["subtitle"] as? String {
self.subtitle = subtitle
}

if let coords = dictionary["coordinates"] as? [String:[String:Double]] {
var latitude = coords.values.first!["latitude"]!
var longitude = coords.values.first!["longitude"]!
var location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
self.coordinate = location
}

}

错误:在隐式生成的 super.init 调用中未初始化属性 self.coordinate。

知道如何解决这个问题吗?

感谢您的一些建议。

最佳答案

您应该在其他情况下分配默认值

if let coords = dictionary["coordinates"] as? [String:[String:Double]] {
...
} else {
self.coordinate = <assign a default location>
}

或者

如果没有坐标,则不应创建对象。所以让你的 init 成为可选的。

 init?(id: String, dictionary: Dictionary<String, AnyObject>){

if let coords = dictionary["coordinates"] as? [String:[String:Double]] {
self.id = id

if let title = dictionary["title"] as? String {
self.title = title
}

if let subtitle = dictionary["subtitle"] as? String {
self.subtitle = subtitle
}
var latitude = coords.values.first!["latitude"]!
var longitude = coords.values.first!["longitude"]!
var location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
self.coordinate = location
} else {
return nil
}
}

关于swift - 自定义 MKAnnotation 字典输入 (CLLocationCooperative2d),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37005077/

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