Nsmanagedobject Setter Giving Me [ Myobject Setname:]: Unrecognized Selector Sent to Instance

NSManagedObject setter giving me [ MyObject setName:]: unrecognized selector sent to instance

From the NSManagedObject Class Reference:

If you instantiate a managed object directly, you must call the
designated initializer
(initWithEntity:insertIntoManagedObjectContext:).

There is also a convenience method

+[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:]

that can be used to create the managed object.

The accessor methods of Core Data objects are created dynamically at runtime,
so one reason for this restriction is that the entity description has to be known.

You can create an object with a nil context and add it to a managed object context later, see for example: How can I associate an NSManagedObject to the context after it has been initialised?

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 NSManagedObjects not an array of Habits.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.

NSManagedObject [User setUsername:]: unrecognized selector sent to instance

You can't create new instances of Core Data entities by the normal alloc-init pattern (unlike other classes). Instead, call a class method on NSEntityDescription:

User *joeBlow = [NSEntityDescription 
insertNewObjectForEntityForName:@"User"
inManagedObjectContext:self.managedObjectContext];
[joeBlow setUsername:@"joeblow"];

See Apple's Core Data Programming Guide for more information.

Finally, note that [jb setUsername:@"jb"] and jb.username = @"jb" are the same - the latter is dot notation, a different way of expressing the former.

[__NSCFSet entity]: unrecognized selector when insertin new nsmanegedobject to an to-many relation

I think it has to do with the fact that the variable account in your addChore: method is not in the state you expect.

NSLog the account variable before adding the chore and see what is there. Maybe it is undefined and you have to make sure it refers to what you intend.

Unrecognized selector when populating an NSManagedObject

You need to create an instance of a sub class of a NSManagedObject using a NSManagedObjectContext based on its NSEntityDescription:

NSManagedObjectContext *managedObjectContext; // Get this from your Core Data stack, probably in the app delegate
HubBrand *brand = [NSEntityDescription insertNewObjectForEntityForName:@"HubBrand" inManagedObjectContext:managedObjectContext];
[brand setBrandName:[NSString stringWithFormat:@"_Custom:, %@", [_txtHubBrand text]]];

See the Creating, Initializing, and Saving a Managed Object section of the doc for more info.

You can also use the sub classes initializer:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"HubBrand" inManagedObjectContext:managedObjectContext];
HubBrand *brand = [[HubBrand alloc] initWithEntity:entity insertIntoManagedObjectContext:managedObjectContext];

But its a bit more wordy!

iOS11 objective-c issue 'unrecognized selector sent to instance' calling a method that exist of a custom class

We had a similar issue. Our exception was this:

-[PDFPasswordViewController setPdfURL:]: unrecognized selector sent to instance 0x101479240

You won't believe this, but we've changed our class name to something that doesn't begin with "PDF".

Previously it was PDFPasswordViewController and we added a prefix, something like XXPDFPasswordViewController. And this is it, now it works as before.

Honestly, we don't know what's causing this, but we think it may be something related to PDFKit that was introduced in iOS 11.

unrecognized selector length when attempting to log entire managed object with overridden getter/setter

I think the problem is that you have overriden memberIds methods.

In your model, memberIds should return a string, and you've overriden it to return an NSArray.
So, when your managedobjet is trying to generate its description, it is assuming that memberIds is a string.

I think the best way to do that it to name your custom getter / setter differently than your model attribute.

CoreData - insertNewObjectForEntityForName can't set attribute

Xcode occasionally goofs up rebuilding changes to xcdatamodel. Next time, try doing a clean build.

Also, you should not hard core the class-name string because it will break refactor.

[NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([Binder class]) inManagedObjectContext:context]

Also, add a prefix to class names and managed objects. (e.g. prefer XYZBinder to just Binder). You will avoid future grief of namespace collisions.

Pass NSManagedObject from One Class to other Class in Swift Programming Language?

You are not supposed to created NSManagedObject instances directly, but instead call:

var employee = NSEntityDescription.insertNewObjectForEntityForName(entityName, inManagedObjectContext: managedObjectContext) as Employee?

and then change employee's properties and save the result.

How can I associate an NSManagedObject to the context after it has been initialised?

This approach would be perfectly valid...

Items *item = [[Item alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
item.first = @"blah";
item.second = @"blah blah";

You're then free to pass this object around to where its needed and when you're ready to commit it to a managed object context, simply insert it and save.

[managedObjectContext insertObject:item];
NSError *error = nil;
[managedObjectContext save:&error];


Related Topics



Leave a reply



Submit