swift - 如何在 MKAnnotation 上显示核心数据中的多个项目

标签 swift xcode core-data mapkit mkannotationview

我正在尝试在来自 Care Data 的 map 注释上显示多个对象。目前,我可以使用此功能获取标题和副标题来显示信息

 func getData() -> [MKAnnotation]? {





        do {
            storedLocations = try context.fetch(Fish.fetchRequest())
            var annotations = [MKAnnotation]()
            for storedLocation in storedLocations {
                let newAnnotation = MyAnnotation(coordinate: CLLocationCoordinate2D(latitude: storedLocation.latitude, longitude: storedLocation.longitude))
                newAnnotation.coordinate.latitude = storedLocation.latitude
                newAnnotation.coordinate.longitude = storedLocation.longitude
                newAnnotation.title = storedLocation.species
                newAnnotation.subtitle = storedLocation.length
                newAnnotation.newTime = storedLocation.time
                newAnnotation.newBait = storedLocation.bait
                newAnnotation.newNotes = storedLocation.notes
                newAnnotation.newWaterTempDepth = storedLocation.water
                newAnnotation.newWeatherCond = storedLocation.weather




              // print(storedLocation.length)
                let newLength = storedLocation.length
                let newTime = newAnnotation.newTime
//                let newBait = storedLocation.bait
//                let newNotes = storedLocation.notes
//                let newWaterTempDepth = storedLocation.water
//                let newWeatherCond = weatherCond

                myLength = newLength!
                myTime = newTime!

//

                annotations.append(newAnnotation)





            }

            return annotations
        }
        catch {
            print("Fetching Failed")
        }
        return nil
    }

我试图在注释上显示所有存储的位置数据。

通过使用此扩展,我发现 here我可以向注释添加多行。 然而,它并非来自核心数据中存储的信息。这就是我为扩展设置 func 的方式

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard !(annotation is MKUserLocation) else {
            return nil
        }

        let reuseIdentifier = "pin"
        var annotationView = map.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)


        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
            annotationView?.canShowCallout = true

            annotationView?.loadCustomLines(customLines: [myLength, myTime])


        } else {
            annotationView?.annotation = annotation
        }


        annotationView?.image = UIImage(named: "fish")


         return annotationView

这是扩展。

 extension MKAnnotationView {

    func loadCustomLines(customLines: [String]) {
        let stackView = self.stackView()
        for line in customLines {
            let label = UILabel()
            label.text = line
            stackView.addArrangedSubview(label)
        }
        self.detailCalloutAccessoryView = stackView
    }



    private func stackView() -> UIStackView {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.distribution = .fillEqually
        stackView.alignment = .fill
        return stackView
    }
}

这是我的注释

    import UIKit
    import MapKit

    class MyAnnotation: NSObject, MKAnnotation {

    var coordinate: CLLocationCoordinate2D
    var title: String?
    var subtitle: String?
    var newLength: String?
    var newTime: String?
    var newBait: String?
    var newNotes: String?
    var newWaterTempDepth: String?
    var newWeatherCond: String?
    var detailCalloutAccessoryView: UIStackView?



      init(coordinate: CLLocationCoordinate2D){
        self.coordinate = coordinate
    }


   }

当我运行时我得到 this

我对编程非常陌生,非常感谢任何帮助,以及对我做错的事情的解释,提前谢谢您

最佳答案

'!'意味着强制展开

myLength = newLength! <- CRASH

由于这里的“newLength”为零,因此在强制解开它的地方将会发生崩溃。

例如您可以修复错误

myLength = newLength ?? 0
myTime = newTime ?? 0

另请参阅 What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?

关于swift - 如何在 MKAnnotation 上显示核心数据中的多个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51366096/

相关文章:

ios - 蓝牙流式传输在后台获取

iphone - 将 SDNestedTable 添加到 subview

core-data - 对多关系的核心数据故障和获取

ios - 在 iOS 核心数据中映射嵌入式对象

arrays - 追加后数组为零

ios - 添加数组以在 Swift : SpriteKit 的 for 循环中移动图像组

ios - 函数返回一个返回函数的函数

ios - 如何告诉静态单元格在 Xcode 中打开特定 View ?

iphone - 如何提高 SQLite3 iPhone 程序的性能

swift - 即使所有参数都正确也无法调用函数