保存谷歌地方对象到核心数据 - 终止与未捕获异常的类型NSException错误
问题描述:
我想使用CoreData来保存和检索用户标记到谷歌地图通过使用自动填充小部件。然而,我不断收到'libC++ abi.dylib:终止与NSException类型的未捕获的异常'错误,我不知道为什么或这意味着什么?当我尝试将对象存储到核心数据中时,会发生这种情况。保存谷歌地方对象到核心数据 - 终止与未捕获异常的类型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
}
}
也许我应该把该让的appDelegate等为viewDidLoad中,但我最终得到了同样的致命错误。
我在StoredPlace实体中设置了名称,地址,placeID属性作为类型'String'。由于未捕获的异常“NSInvalidArgumentException”,原因 终止应用程序:
有关该错误“+ entityForName:无不是合法的NSManagedObjectContext参数搜索实体名称“StoredPlace” ***第一掷调用堆栈:
这个错误是什么意思,我如何解决它?
答
我已经完成了这项工作,这是相当愚蠢的,但是很容易让错误向其他人提供。如果在创建xcdatamodel后重命名它,不要忘记在Appdelegate中更改容器的状态。因此'let container = NSPersistentContainer(name:“YOUR RENAMED FILE”)',否则会出现上述错误!
答
其中是您的核心数据方法?在这个ViewController或AppDelegate?
这是ViewController。我将更新帖子以显示AppDelegate方法。我正在使用最新版本的Xcode和Swift –
哦,天哪,谢谢你的问题,因为事实证明,我忘了更改容器名称以匹配我的xcdatamodel,因为我在创建后重命名它.... –