NSManagedObject does not validate isKindOfClass

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

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"); } 

Exit:

 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

Description print classClass prints "DayModel."

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


UPDATE

This does not work in my test class, but not in the iOS app. This problem seems similar to this problem . However, I have added all the classes that I can use for the test purpose, and still do not work.

+5
source share
3 answers

I had the same problem.

The problem in my case was actually NOT that I was not in the source file in the test project, as you mentioned in your update at the link: isKindOfClass non-stop returns NO

The root cause was caused by too many source files with the same class. In the test goal, you probably have a target dependency on your goal containing your application, i.e. You already have the source file.

Therefore, you must remove the source file containing the class that you use in isKindOfClass from Compile Sources for the test purpose on the Phase Assembly tab.

(In your case, delete DayModel.m)

I found a solution to my problem here: isKindOfClass and NSStringFromClass do not agree with UIApplicationDelegate

It seems that if there are multiple source files with the same class, isKindOfClass has odd behavior, because it cannot see that the two classes are the same.

+5
source

I hit my head about it alone for several hours, and all I could find on the Internet was Targets. Turns out I didn't set the Class field in the xcdatamodeld editor, it was still "NSManagedObject" when it was supposed to be the class name ...

Verify that the class name is in the Name field and the Class field in the data model inspector (Cmd-Opt-3).

+4
source

For managed objects, it is better to use this method to verify the class:

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

Source: https://habr.com/ru/post/1210228/


All Articles