保存 google 将对象放置到 Core Data - 以 NSException 类型的未捕获异常终止

Saving google places objects to Core Data - terminating with uncaught exception of type NSException error

我正在尝试使用 CoreData 保存和检索用户通过使用自动完成小部件在 Google 地图上标记的标记。但是,我不断收到 'libc++abi.dylib: terminating with uncaught exception of type NSException' 错误,我不确定这是为什么或这意味着什么?当我尝试将对象存储到核心数据中时,就会发生这种情况。

相关代码如下:

    func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
    self.place = place
    let camera = GMSCameraPosition.camera(withLatitude: place.coordinate.latitude, longitude: place.coordinate.longitude, zoom: 15.0)
    self.vwGMap.camera = camera
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(place.coordinate.latitude, place.coordinate.longitude)
    marker.title = place.name
    marker.snippet = place.formattedAddress
    marker.map = self.vwGMap
    marker.icon = GMSMarker.markerImage(with: UIColor.blue)
    marker.tracksViewChanges = true
    self.dismiss(animated: true, completion: nil)
    print("Place name: ", place.name)
    print("Place address: ", place.formattedAddress)
    print("Place placeID: ", place.placeID)
    print("Place attributions: ", place.attributions)
    print("Place Coordinate:", place.coordinate)
    //The printing only happens in the terminal

    let newPlaceName = place.name
    self.newPlaceName = place.name
    let newPlaceAddress = place.formattedAddress
    self.newPlaceAddress = place.formattedAddress!
    let newPlacePlaceID = place.placeID
    self.newPlacePlaceID = place.placeID

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    let context = appDelegate.persistentContainer.viewContext
    let newPlace = NSEntityDescription.insertNewObject(forEntityName: "StoredPlace", into: context)

    newPlace.setValue(newPlaceName, forKeyPath: "name")
    newPlace.setValue(newPlaceAddress, forKeyPath: "address")
    newPlace.setValue(newPlacePlaceID
        , forKeyPath: "placeID")

    do
    {
        try context.save()
        print("SAVED")
    }
    catch
    {
        //PROCESS ERROR
    }


}

也许我应该将 let appDelegate 等放入 viewDidLoad 中,但我最终得到了同样的致命错误。

我在 StoredPlace 实体中将名称、地址、地点 ID 设置为 'String' 类型的属性。

关于错误的更多信息: 由于未捕获的异常 'NSInvalidArgumentException' 而终止应用程序,原因:“+entityForName:nil 不是搜索实体名称的合法 NSManagedObjectContext 参数 'StoredPlace'” *** 首先抛出调用栈:

这个错误是什么意思,我该如何解决?

你的核心数据方法在哪里?在此 ViewController 或 AppDelegate 中?

我现在已经解决了这个问题,这很愚蠢,但很容易犯这样的错误,仅供参考。如果在创建 xcdatamodel 后重命名它,请不要忘记在您声明 container.So 'let container = NSPersistentContainer(name: "YOUR RENAMED FILE")' 的 Appdelegate 中更改它,否则您会收到上述错误!