5

I have an NSManagedObject. When I create an instance, it fails the isKindOfClass method unexpectedly.

NSEntityDescription *entity = [NSEntityDescription entityForName:@"DayModel" inManagedObjectContext:context];
DayModel *day = [[DayModel alloc] initWithEntity:entity insertIntoManagedObjectContext:context];

if ([day isKindOfClass:[DayModel class]]) {
    NSLog(@"True");
} else {
    NSLog(@"False");
}

Output:

False

I added the following code:

Class objectClass = [day class];
Class classClass = [DayModel class];

And looking at it in the debugger this is what I found:

enter image description here

Printing the description of classClass prints "DayModel."

I'm not sure this is relevant, but DayModel is implemented in Swift.


UPDATE

This is failing in my test class, but not in the iOS app. The problem seems similar to this issue. However, I've added all the classes I can to the test target and it is still failing.

Community
  • 1
  • 1
rob
  • 4,069
  • 3
  • 34
  • 41
  • 1
    Not sure, but this might help... http://stackoverflow.com/a/12161219/2274694 – Lyndsey Scott Jan 01 '15 at 20:51
  • Thanks, I think that's close. This is failing in a test class, but working in the app. I've added all the classes to the target though... – rob Jan 01 '15 at 21:15
  • In the model editor, the class name has to be xxx.DayModel where xxx is the name of your app module. Is this the case? – jrturton Jan 01 '15 at 23:19
  • @jrturton, Do you mean modifying the xcdatamodeld file, selecting the DayModel, and in the Utility panel on the right change the class name? Right now it is just DayModel, and if I change it like you suggest the app crashes. Is there an example online that could help me? Thanks. – rob Jan 02 '15 at 03:33
  • That's interesting. Crashes how? What's your module name? – jrturton Jan 02 '15 at 05:26
  • Also, DayModel needs to be declared as `public class DayModel`, have you done that? I'm not sure what you mean by "adding classes to the test target", that shouldn't be necessary. – jrturton Jan 02 '15 at 05:31

3 Answers3

5

I just had the same problem.

The problem in my case was actually NOT that I was missing the source file in the test project as you mention in your update with the link: isKindOfClass returning NO unexpectedly

The root cause was due to too many source files with the same class. In your test target you probably have a target dependency to your target containing your application, i.e. you already have the source file included.

Hence ensure to remove the source file containing the class you are using in isKindOfClass from 'Compile Sources' for the test target in the 'Build Phases' tab.

(In your case remove DayModel.m)

I found the solution to my problem here: isKindOfClass and NSStringFromClass disagree about UIApplicationDelegate

It seems that when having multiple source files with the same class the isKindOfClass has odd behavior, since it cannot see the two classes as being the same.

Community
  • 1
  • 1
dynamokaj
  • 471
  • 7
  • 19
  • Thanks for the suggestion @dynamokaj. However, If I remove the DayModel from compile sources in the test target, I get a compilation error because DayModel is no longer visible to the test class. – rob Mar 19 '15 at 18:54
4

Been banging my head against this one for hours, and all I could find on the web was the Targets thing. Turns out I had not set the "Class" field in the xcdatamodeld editor, it was still "NSManagedObject" when it should have been the name of the class...

Check to make sure the the class name is in both the "Name" field AND the "Class" field in the Data Model Inspector (Cmd-Opt-3).

Desco
  • 41
  • 2
0

For managed objects is better to use this way to check the class:

if ([object.entity.name isEqualToString:NSStringFromClass(MyManagedObjectSubclass.class)]) {

}
93sauu
  • 3,770
  • 3
  • 27
  • 43