CoreData setting relationship gives error '-[WeatherObservation count]: unrecognized selector sent to instance
Had the same problem. Error occurred even after clean and rebuild.
Double checked the relation and it was indeed One to One and I still had the same error.
The solution: delete the "hiveObservation" relation and his counterpart in the corresponding Entity (if present) and them add new ones with the same names.
CoreData Unrecognized Selector Sent to Instance
You say the problem occurs when adding and saving a new Album
, though I think the issue actually relates to Photos
. The error:
reason: '-[__NSConcreteUUID compare:]: unrecognized selector sent to instance
is in old Objective-C terminology (CoreData is at its heart Objective-C based). It indicates that there is an object of type __NSConcreteUUID
(which is presumably the type of object that CoreData uses to store your UUIDs) and CoreData is trying to call the compare
method on that object. compare
is the method that gets called when sorting objects: compare object A with object B to work out what order they should be placed relative to each other.
So, the source of the problem is that your views are sorting something by UUID, and that appears to be the Photos:
@FetchRequest(entity: Photo.entity(),
sortDescriptors: [
NSSortDescriptor(keyPath: \Photo.id, ascending: true)
]
) var photos: FetchedResults<Photo>
but UUIDs cannot be sorted in that way. Unless there is good reason for sorting by UUID, I would just change the sort order to use a different attribute.
unrecognized selector sent to instance with Coredata Swift
Ok so the reason you are getting the error is most certainly because the object referenced by self.habit
is not a Habit
object. The easiest way to find out what the object really is is to call:
print(NSStringFromClass(habit.class))
With core data and custom NSManagedObjects
you need to make sure that the entity: 'Habit' (in your data model) has a class set to Habit. This makes sure that Core Data casts your fetched objects with an entity description of 'Habit' to the Habit class. If you are not doing this then the getHabits
func will be returning an array of NSManagedObject
s not an array of Habit
s.If this is the case then the code: println(NSStringFromClass(habit.class))
will print "NSManagedObject" to the debugger.
As a side note, you really need to check for errors when you fetch objects from a Core Data database. Add the lines:
if objects? == nil {
print("An error occurred \error.localisedDescription")
}
Please forgive my swift if there are any errors, I normally use Objective-C.
EDIT: In order to correct Failed to call designated initializer on NSManagedObject class 'X'
error. This error is fired when you do not correctly instantiate an NSManagedObject
. You must not call [[MyManagedObject alloc] init];
you have to call initWithEntity:insertIntoManagedObjectContext
instead:
MyManagedObject *obj = [[MyManagedObject alloc] initWithEntity:[NSEntityDescription entityForName:@"MyManagedObject" inManagedObjectContext:context] insertIntoManagedObjectContext:context];
If you do not want the object obj
to be inserted into a context you can pass through a nil
context argument. However, if you want undo management and the ability to save the object to the database it needs to be associated with a context.
If you want to have a custom initialisation of an object then you can override the awakeFromInsert
and awakeFromFetch
methods/functions.
Core data exception: initWithCoder:]: unrecognized selector sent
The solution was pretty simple.
User had also another relationship with a transformable container (NSArray) which was holding (for a bug) a PharmaComp instance.
Getting the error unrecognized selector sent to instance with core data and swift
Turns out there was an inconsistancy between my xcdatamodeld
file and my objects. Thanks to everyone who spent time on this problem.
Cheers!
CoreData and NSInvalidArgumentException unrecognized selector sent to instance
The problem is that you are trying to instantiate a NSManagedObject
using a conventional alloc/init method of NSObject
.
If you want to use CoreData you have to use the NSManagedObject designated initialiser, as described on the class reference documentation for NSManagedObject
NSManagedObject is a generic class that implements all the basic
behavior required of a Core Data model object. It is not possible to
use instances of direct subclasses of NSObject (or any other class not
inheriting from NSManagedObject) with a managed object context. You
may create custom subclasses of NSManagedObject, although this is not
always required. If no custom logic is needed, a complete object graph
can be formed with NSManagedObject instances.A managed object is associated with an entity description (an instance
of NSEntityDescription) that provides metadata about the object
(including the name of the entity that the object represents and the
names of its attributes and relationships) and with a managed object
context that tracks changes to the object graph. It is important that
a managed object is properly configured for use with Core Data. If you
instantiate a managed object directly, you must call the designated
initializer (initWithEntity:insertIntoManagedObjectContext:).
Source: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSManagedObject_Class/Reference/NSManagedObject.html
Related Topics
Url Constructor Doesn't Work with Some Characters
Unrecognized Selector Sent to Instance When No Related Entities Found in Core Data
Uicollectionviewcell Not Forcing Cell Size
String Nil Inside Nsurlsession
Macos Playground: How Can Change the Background Color
@Published Property Not Triggering Anything
Swift MACos Popover Detect Change Dark Mode
How to Define an Array of a Particular Enum Case in Swift
Swift Generic Func Cannot Convert Value of Type to Expected Argument Type
Swift Interstitial Making a Banner Ad
Display Data in Two Different Tableview in the Same View in Swift
Update Text in While Loop Swift
Swift Nsunknownkeyexception - Setvalue:Forundefinedkey
Check If on Correct Dispatch Queue in Swift 3
Autoscrolling Infinite Effect in .Linear Type of Icarousel in Swift