How come I can cast to NSManagedObject but not to my entity's type?
Make sure your Class name field is actually Module.Task, where Module is the name of your app. CoreData classes in Swift are namespaced. Right now, your object is being pulled out of the context as an NSManagedObject, not as a Task, so the as-cast is failing.
Core Data Migration: Could not cast value of type 'NSManagedObject_MyType' to 'MyModule.MyType'
The reason is that during migration, you should not be using instances of NSManagedObject subclasses. You need to express all of these in the form of NSManagedObject. So the code above must become:
class LegacyToModernPolicy: NSEntityMigrationPolicy {
static func find(entityName: String,
in context: NSManagedObjectContext,
sortDescriptors: [NSSortDescriptor],
with predicate: NSPredicate? = nil,
limit: Int? = nil) throws -> [NSManagedObject] {
let fetchRequest: NSFetchRequest<NSManagedObject> = NSFetchRequest(entityName: entityName)
fetchRequest.predicate = predicate
fetchRequest.sortDescriptors = sortDescriptors
if let limit = limit {
fetchRequest.fetchLimit = limit
}
do {
let results = try context.fetch(fetchRequest)
return results
} catch {
log.error("Error fetching: \(error.localizedDescription)")
throw error
}
}
func libraryForManager(_ manager: NSMigrationManager) -> NSManagedObject {
do {
var library: NSManagedObject? = try LegacyToModernPolicy.find(entityName: Library.entity().name!,
in: manager.destinationContext,
sortDescriptors: [NSSortDescriptor(key: "filename", ascending: true)],
with: nil,
limit: 1).first
if library == nil {
let dInstance = NSEntityDescription.insertNewObject(forEntityName: Library.entity().name!, into: manager.destinationContext)
// awakeFromInsert is not called, so I have to do the things I did there, here:
dInstance.setValue(Library.libraryFilename, forKey: #keyPath(Library.filename))
dInstance.setValue(NSDate(timeIntervalSince1970: 0), forKey: #keyPath(Library.updatedAt))
library = dInstance
}
return library!
} catch {
fatalError("Not sure why this is failing!")
}
}}
You can read more about my less-than-fun experiences with Core Data Migrations here.
Problems casting NSManagedObject to subclass type
The cast isn't your problem. Casting doesn't change what class an object is — just what the compiler thinks it is.
You object is an NSManagedObject but not a Contact
instance. In you code the object
you have is a Hospital
entity. Double-check that the Hospital
entity is set to use the Contact
class (or a subclass)).
It is important to note that Entity inheritance and Objective-C class inheritance don't have to match. I.e. you can have Hospital
be a subentry of Contact
and still have the Contact
entity map to a Contact
class without Hospital
map to a subclass of the Contact
class. It is valid for the Hospital
entity to map to the (same) Contact
class or even to NSManagedObject
(which is what I suspect you've done).
This may seem confusing but can be very powerful if used correctly.
Core Data NSManagedObject downcast with XCode 7 and Swift
Go to your cdatamodel..where you declared your entity..There is a Default Configuration below the Entity. Check the Class column against your entity. By default this will have a dot prefixed. Remove the dot prefix .And it should work. And from what i see is you do not have to prefix your entity class name with the module name in Xcode 7. Hence you cannot use a dot in the class name.I am a newbie in iOS development. So i might not be totally right. But removing the dot prefix solved my issue in Xcode 7.
Related Topics
How to Tap on a Specific Point Using Xcode Uitests
Memory Leak Every Time Uiscrollview Is Released
Eraser Not Working in iOS Drawing
Multiline Label in Uistackview
How to Draw a Circle in iOS Swift
Code Signing Is Required for Product Type Unit Test Bundle in Sdk iOS 8.0
How to Make Uiimagepickercontroller for Camera and Photo Library at the Same Time in Swift
iOS Cllocationmanager in a Separate Class
Fbsdkloginmanager Loginwithpublishpermissions Always Returns Iscancelled=Yes
Uitableviewcell Imageview Changing on Select
Convert Request Function to Generic Type
How to Invoke iPhone Maps for Directions with Current Location as Start Address
How to Implement a Box or Gaussian Blur on iOS
Check If Location Services Are Enabled
Open Uisplitviewcontroller to Master View Rather Than Detail
How to Programmatically Use iOS Voice Synthesizers? (Text to Speech)