How to define the selected property in NSManagedObject subclass

In a project I am working on, we have several persistent stores and selected properties are defined in Entities to allow access to objects that live in different stores.

When I run the Editor

Create NSManagedObject Subclass

, the selected properties are not filled in the subclass, and therefore, not available in different controllers that use the Entity.

My curiosity is how to define these objects in a subclass so that they can be used.

For example, imagine I have some object called "Some Object" and that object has a fetchched property called "imageFile" (the File object lives in a different store, so cannot be referenced directly)

class SomeObject: NSManagedObject {

   @NSManaged var name: String
   @NSManaged var id: String
   @NSManaged var imageID: String
   @NSManaged var imageFile: File             //Not generated automatically like the rest
}

      

Unfortunately, the above attempt failed with the following error:

unrecognized selector posted to instance 0x60800865de50

So my question in a nutshell is how do you access the Fetched Properties or what is the syntax for referencing them.

Please do not answer the question "Do not use the selected properties" or "Use only one persistent store". I already know how to use normal relationships and want to know how to use this Core Data feature. Thanks in advance!

UPDATE

After trying some solution posted below, I came across some interesting information that might help. I printed out the object using "po someObject" and was surprised to see the following in the data attribute in the output:

imageFile = "<relationship fault: 0x618000043930 'imageFile'>";
imageID = "some Id"

      

However, when trying to access the imageFile using someObject.imageFile, I cannot access it. Using valueForKey ["imageID"] I can get the link, but it doesn't fire when passed to the file every time. When I print an object, I get:

Optional(Relationship fault for (<NSFetchedPropertyDescription: 0x6180000e1780>), name imageFile, isOptional 1, isTransient 1, entity SomeObject...

      

Final update

valueForKey["imageID"]

will throw an error and retrieve the property, I had the attributes reversed in my xcdatamodelid

file and so it didn't find it at first.

+3


source to share


2 answers


In Objective-C, you can define a properties file @dynamic

in a category on SomeObject

, but something like this doesn't exist in Swift (as far as I know).

So the only possibility is to use the Key-Value encoding to retrieve (which is always represented as an array):



if let files = yourObject.valueForKey("imageFile") as? [File] {
    // ...
}

      

You can of course wrap this in a computed property as suggested in @ gutenmorgenuhu's answer.

+3


source


If you want to add this to the same class as NSManagedObject, you can use the extension function:



extension SomeObject{
  var imageFile: String {
    get {// Code to return your fetchedProperty
    }
  }
}

      

+2


source







All Articles