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:
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With