Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

core data errors adding new record

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!

like image 837
Dandan Avatar asked Jul 31 '26 16:07

Dandan


1 Answers

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).

like image 191
pbasdf Avatar answered Aug 03 '26 04:08

pbasdf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!