When I run the following code:
    let saveAction = UIAlertAction(title: "Save",
                                   style: .default) {
    [unowned self] action in
    guard let textField = alert.textFields?.first,
    let mapName = textField.text else {
    return
    }
    var newCoordinate = NSEntityDescription.insertNewObject(forEntityName: "Coordinates", into: managedObjectContext)
    var newMap = NSEntityDescription.insertNewObject(forEntityName: "MapNames", into: managedObjectContext)
    newCoordinate.setValue(23, forKey: "latitude")
    newCoordinate.setValue(21, forKey: "longitude")
    newMap.setValue(mapName, forKey: "mapname")
    do
    {
    try self.appDelegate.saveContext()
    print("SAVED")
    }
    catch
    {
    }
    var request = NSFetchRequest<NSFetchRequestResult>(entityName: "Coordinates")
    request.returnsObjectsAsFaults = false
    do
    {
    var results = try managedObjectContext.fetch(request)
        if results.count > 0 {
        for result in results as! [NSManagedObject]
        {
            if let latitude = result.value(forKey: "latitude") as? Double
            {
            print(latitude)
            }
            if let longitude = result.value(forKey: "longitude") as? Double{
            print(longitude)}
            if let map = result.value(forKey: "mapname") as? String {
            print(map)}
        }
    }
    }
    catch
    {
    }
I get the following error:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: the entity Coordinates is not key value coding-compliant for the key "mapname".'
This is weird because I do have an attribute called mapname, although it is in the MapNames entity. The error says Coordinates, why is that?
Is that because it is not possible to have 2 references to managedObjectContext in the same scope? How could I insert values to different entities then?
if let map = result.value(forKey: "mapname") as? Stringis obviously trying to read propertymapnameon an instance ofCoordinatesbecause that's what your request is fetching. That should be obvious if you applied the generic types correctly. Your request should beNSFetchRequest<Coordinate>.