我在Swift上使用Xcode 6.1.1。該項目使用的核心數據,以節省目擊添加新記錄的核心數據錯誤
import Foundation
import CoreData
@objc(Sighting)
class Sighting: NSManagedObject {
@NSManaged var lat: Double
@NSManaged var lng: Double
@NSManaged var seen_at: NSDate
}
在IBAction爲我創建一個新的瞄準
@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
...
}
就行了「讓瞄準=」我的錯誤swift_dynamicCastClassUnconditional。任何想法爲什麼?
更換符合
let entityDescripition = NSEntityDescription.entityForName("Sighting", inManagedObjectContext: managedContext)
let sighting = Sighting(entity: entityDescripition!, insertIntoManagedObjectContext: managedContext)
似乎讓過去的問題,但那麼它只是導致下一行「sighting.lat =」給錯誤EXC_BAD_ACCESS> objc_msgSend
coordinate.latitude是正確返回一個CLLocationDegrees
任何想法如何解決這個將不勝感激。謝謝!
確實標記類公共:'公共類瞄準:NSManagedObject'幫助? –