3

I'm using Xcode 6.1.1 with Swift. The project is using core 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 an 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
    ...
}

On the line "let sighting =" I get the error swift_dynamicCastClassUnconditional. Any ideas why?

Replacing the line with

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

seems to get past the issue but then it just results in the next line "sighting.lat =" giving the error EXC_BAD_ACCESS > objc_msgSend

coordinate.latitude is correctly returning a CLLocationDegrees

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

Dandan
  • 650
  • 1
  • 10
  • 17

1 Answers1

2

I suspect your Model does not link the entity "Sighting" with the class Sighting. Select the entity in the model editor, and enter the class name in the data model inspector panel on the right:

screenshot

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

pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • Awesome. I didn't have a class name in the class input. Adding just "Sighting" without the app name seemed to work fine. Thanks! – Dandan Jan 12 '15 at 02:07