gpt4 book ai didi

ios - 无法访问核心数据

转载 作者:行者123 更新时间:2023-11-30 12:02:48 25 4
gpt4 key购买 nike

我正在开发一个运行应用程序,由于某种原因,当我尝试访问数据以将其映射出来时,出现错误sudo gem install cocoapods-deintegrate cocoapods-clean

fatal error :在解包可选值时意外发现 nil

线程 1:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)位于以下行

let locations = run.locations

如果我把它改成

let locations = run?.locations

我在应用程序中收到“else{”消息,但它没有崩溃。

我知道数据在那里,因为我与 Liya 确认过。不知道为什么我的应用程序看不到它。

这是其余的代码

    var run : Run!

func loadMap() {
guard
let locations = run.locations,
locations.count > 0,
let region = mapRegion()
else {
let alert = UIAlertController(title: "Error",
message: "Sorry, this run has no locations saved",
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel))
present(alert, animated: true)
return
}
print("loadMap")
mapView.setRegion(region, animated: true)

mapView.add(polyLine())
}

TIA

最佳答案

布埃诺!谢谢。

好的。所以就在这里

已删除

    var run : Run!

声明一个空数组,因为获取返回 [Any]

    var locations : [Any] = [] 

添加委托(delegate)

let appDelegate = UIApplication.shared.delegate as? AppDelegate

然后将获取数据函数编写为带有完成处理程序的扩展

extension MapsViewController {
func fetch(completion: (_ complete: Bool) -> ()) {
guard let managedContext = appDelegate?.persistentContainer.viewContext else { return }

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Location")

do{
locations = try managedContext.fetch(fetchRequest) //Trys to call CoreData and returns an Array of type [Any] and assigns them to the array of locations
print("fetched data")
completion(true)

} catch {
debugPrint("could not fetch: \(error.localizedDescription)")
completion(false)
}
}

关于ios - 无法访问核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47004048/

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