Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

executeFetchRequest doesn't return the NSManagedObject subclass

Tags:

swift

xcode6

This is for XCode 6 and Swift...

I'm trying to make a fetch request to the managed object context but it's not returning the correct subclass.

I have already set the subclass in the data model data modeler configuration to the name of my custom subclass and in the code, it is extending the NSManagedObject class.

Any ideas?

enter image description hereenter image description hereenter image description here

like image 500
vutran Avatar asked Jun 06 '14 01:06

vutran


3 Answers

Just figured out the solution.

I had to add the @objc attribute to allow the class to be compatible with Objective-C.

Now the fetch request is returning a correct result of Tasks[]

import Foundation
import CoreData

@objc(Task) // make compatible with objective-c
class Task : NSManagedObject
{
    @NSManaged var note: String!
    @NSManaged var completed: Bool
}

Reference: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithObjective-CAPIs.html#//apple_ref/doc/uid/TP40014216-CH4-XID_36

like image 62
vutran Avatar answered Nov 19 '22 03:11

vutran


Using @objc(Task) seems to be working but you could also just edit the data model data modeler configuration to the name ToDoList.Task instead of just Task. That will work too and avoid Class conflicts if Task is used anywhere else in the Objective-C code.

like image 38
Fabien Penso Avatar answered Nov 19 '22 03:11

Fabien Penso


Check to make sure that in the "Entity" inspector (right side of the screen, Utilities pane) when Task is selected in your Model that its Class field is properly filled in with "Task" (it's blank by default).

like image 1
Ben Gottlieb Avatar answered Nov 19 '22 03:11

Ben Gottlieb