Major data errors adding a new record

I am using Xcode 6.1.1 with Swift. The project uses master data to save Sightings

import Foundation
import CoreData
@objc(Sighting)

class Sighting: NSManagedObject {
    @NSManaged var lat: Double
    @NSManaged var lng: Double
    @NSManaged var seen_at: NSDate
}

      

In IBaction I create a new Sighting

@IBAction func addSighting(sender: AnyObject) {
    let coordinate = locationManager.location.coordinate
    let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let managedContext = appDelegate.managedObjectContext!
    let sighting = NSEntityDescription.insertNewObjectForEntityForName("Sighting", inManagedObjectContext: managedContext) as Sighting

    sighting.lat = coordinate.latitude
    sighting.lng = coordinate.longitude
    ...
}

      

The swift_dynamicCastClassUnconditional error appears on the "let sighting =" line. Any ideas why?

Replacing a string with

    let entityDescripition = NSEntityDescription.entityForName("Sighting", inManagedObjectContext: managedContext)
    let sighting = Sighting(entity: entityDescripition!, insertIntoManagedObjectContext: managedContext)

      

seems to get past the problem, but then it just leads to the next line "sighting.lat =" giving error EXC_BAD_ACCESS> objc_msgSend

constant.latitude correctly returns CLLocationDegrees

Any ideas on how to resolve this would be appreciated. Thank!

+3


source to share


1 answer


I suspect your model does not bind the "Sighting" entity to the class Sighting

. Select the object in the model editor and enter the class name in the data model inspector pane on the right:

screenshot



Note that in Swift, the class name must include the name of your module (application).

+2


source







All Articles