gpt4 book ai didi

Swift:CoreData 符合 MKAnnotation

转载 作者:行者123 更新时间:2023-11-30 11:34:00 26 4
gpt4 key购买 nike

我正在尝试实现我的自定义 CoreData Carpark 实体以符合 MKAnnotation,就像我们如何使 class 对象符合 >MKAnnotation.

我根据以下帖子调整了我的实现:this , this .

到目前为止我的实现:

//At Carpark+CoreDataClass.swift
import Foundation
import CoreData
import Mapkit

@objc(Carpark)
public class Carpark: NSManagedObject, MKAnnotation {

var coordinate: CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: Double(self.lat), longitude: Double(self.long))
}

var title: String {
return self.carparkName
}

}

//At Carpark+CoreDataProperties.swift
import Foundation
import CoreData
import Mapkit

extension Carpark {

@nonobjc public class func fetchRequest() -> NSFetchRequest<Carpark> {
return NSFetchRequest<Carpark>(entityName: "Carpark")
}

@NSManaged public var carparkName: String?
@NSManaged public var sundayAndPublicHolRate: String?
@NSManaged public var saturdayRate: String?
@NSManaged public var weekdayRate1: String?
@NSManaged public var weekdayRate2: String?
@NSManaged public var lat: Double
@NSManaged public var long: Double
}

//At VC where I addAnnotations
var allCarparks: [Carpark]?
var nearbyCarparks = [Carpark]()
var searchedCarpark: Carpark!

func filterCarparksWithinRadius() {

var allAnnotations = [Carpark]()

let searchRadius = UserDefaults.standard.object(forKey: Constants.UserDefaults.SearchRadius) as! Double
let searchedCarparkLocation = CLLocation(latitude: searchedCarpark.lat, longitude: searchedCarpark.long)

allCarparks?.forEach({ (carpark) in
let carparkLocation = CLLocation(latitude: carpark.lat, longitude: carpark.long)
let distance = carparkLocation.distance(from: searchedCarparkLocation)

if distance <= searchRadius && distance != 0 {
nearbyCarparks.append(carpark)
allAnnotations.append(carpark)
}
})

allAnnotations.append(searchedCarpark)
mapView.addAnnotations(allAnnotations as! [MKAnnotation]) //Problem here

let zoomCoordinate = CLLocationCoordinate2D(latitude: searchedCarpark.lat, longitude: searchedCarpark.long)
zoomToLocation(coordinate: zoomCoordinate)

}

上面的代码会抛出错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Carpark coordinate]: unrecognized selector sent to instance 0x60400009e690'

有没有一种方法可以让 CoreData 符合 MKAnnotation,就像我们对类对象所做的那样?类似于这样的东西?:

class Carpark: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?

var carparkName: String?
var lat: Double?
var long: Double?

init(carparkName: String?, lat: Double?, long: Double?) {
self.title = carparkName
self.carparkName = carparkName
self.lat = lat
self.long = long
self.coordinate = CLLocationCoordinate2DMake(lat!, long!)
}
}

非常感谢任何建议,谢谢。

最佳答案

您需要将坐标属性设为公共(public),以便在模块外部(如 MapView 框架中的外部)可见。也许这也是您需要转换数组的原因。

这是我的定义

public class Location: NSManagedObject, MKAnnotation {

public var coordinate: CLLocationCoordinate2D {
return CLLocationCoordinate2DMake(latitude, longitude)
}
//....more code
}

在我的 View Controller 中我有一个属性

 var locations = [Location]()

我将其获取并添加到 map View 中,如下所示

private func updateLocations() {
mapView.removeAnnotations(locations)

let entity = Location.entity()
let fetchRequest = NSFetchRequest<Location>()
fetchRequest.entity = entity
do {
locations = try managedObjectContext.fetch(fetchRequest)
} catch {
fatalCoreDataError(error)
}
mapView.addAnnotations(locations)
}

关于Swift:CoreData 符合 MKAnnotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49953738/

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