0

I am searching how to get not managed property names and types of a NSManagedObject subclass.

here is few sample code to help me to ask my question :

 @objc(Operation)
    public class Operation : NSManagedObject {
         @NSManaged var name: String
         @NSManaged var amount: NSNumber
      }


  @objc(Account)
    public class Account: NSManagedObject {
        @NSManaged var bic: String
        @NSManaged var number: String

        @NSManaged var operations: Set<Operation>
        @NSManaged var servicesSubscriptions: Set<ServiceSubcription>

        // and more.
    }

    extension Account
    {

    public var lastOperation : Operation {
        get
        {
            return self.operations.last
        }
        set(value) 
        {    
            self.operations.insert(value)
        }
      }
    }

I have found many ways to get property names using reflect() function. reflect() do not work with NSManagedObject at all. (like this simple one)

edit I have found examples with class_copyPropertyList function, that retrieve correctly property names, but don't found yet how to get types. Thank to Tom Harrington comment. (see that sample)

I have found many ways to get Attributes (or relations) of managed objects using NSEntityDescription. (like this one). Which work and get back bic and number, but not lastOperation.

edited updated code sample to match better to reality

So my question is : How to get back my lastOperation property, and its type, dynamically at run time ?

edit, what i am trying to do I am parsing json, dnamically using reflection. I need the type (or type name) of a property knowing only its name (i have "lastOperation", and need to get back Operation, or "Operation"). Once i get the type i can instanciate an object, then populate its own properties, using same mechanism (recursively).

Thank you for any help

Community
  • 1
  • 1
Romain TAILLANDIER
  • 1,765
  • 10
  • 18
  • Why do you need to get it dynamically? What are you actually trying to do with it? – Wain Aug 12 '15 at 14:28
  • `class_copyPropertyList` should work just fine with `NSManagedObject`. What problem(s) did you have? – Tom Harrington Aug 12 '15 at 18:12
  • My goal is to parse Json dynamically, and build complex object containing complex object in it, using reflection. It works pretty good execpt a few specific cases. (i will update the post to explain more) I finally get class_copyPropertyList work and retrieve property names, but do not find how to get property type from its name. – Romain TAILLANDIER Aug 13 '15 at 07:44

2 Answers2

0

There is no way that I know of when it comes to a NSManagedObject. However, I would suggest creating title as a transient property inside of your model and then it will show up as part of the entity description.

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
  • I have take a look on transient attribute. It looks it could do the job, if i can make it transformable to my Operation class. At a first glance it looks complicated. [like explained in this post](http://davemeehan.com/technology/objective-c/core-data-transient-properties-on-nsmanagedobject) – Romain TAILLANDIER Aug 13 '15 at 09:27
  • Transient is not transformable. A transient property is what you already have, you are just declaring its existence in the data model. There is ZERO additional code. Most of that post was speaking towards updating the transient property when other properties change. – Marcus S. Zarra Aug 13 '15 at 13:30
  • Tom Harrington reply seems more adapted to me. But i want to test yours deeper. How do you set the type of transient property to 'Operation' in the model editor ? For no i get nothing for the type, when running the code, with only the transient box checked. – Romain TAILLANDIER Aug 14 '15 at 07:11
0

When you get the list of properties using class_copyPropertyList, you can iterate through the list to look at each property in turn:

var propertyCount : UInt32 = 0
let properties = class_copyPropertyList(Account.self, &propertyCount)

for var i=0; i<Int(propertyCount); i++ {
    let property = properties[i]
    let propertyName = String(UTF8String: property_getName(property))
    ....
}

The type of each property is contained in one of the property attributes, as a string:

    let propertyType = property_copyAttributeValue(property, "T")
    let propertyTypeString = String(UTF8String: propertyType)

For your lastOperation property the string will look something like @\"Operation\". You'll have to clean up that string a little to get Operation.

I wrote a blog post a while ago describing something similar to what you're trying to do. The code is in Objective-C but all the functions, methods, etc are the same.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • Your code do not convert correctly the CString, this one is OK : var propertyType = property_copyAttributeValue(property, "T") let propertyTypeString = String.fromCString( propertyType) This will solve my problem. Thank you ! (may i suggest to update your reply ?) – Romain TAILLANDIER Aug 14 '15 at 07:05
  • Converting from UTF8 worked when I tried it, and as far as I know should be equivalent to converting from C strings in this case. What specifically was wrong when you tried it? – Tom Harrington Aug 14 '15 at 15:46