Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective c type casting an object based on an NSString class name

Tags:

ios

iphone

I have an issue where I am getting in an object of type id. i know its base type is an NSManagedObject but the custom attributes for whatever type it actually is (name, email, etc) are what i really need. in the long run, i am trying to make one method that will turn my custom NSManagedObjects into NSDictionaries. maybe i am going down the wrong path in general but....

If i have something like

-(void)someMethod:(id)obj{
...
Class someClass = NSClassFromString([[obj class] description]);
...

i can get the class from that but then i cannot really figure out how to cast my object as that class.

ideally this would be the next few lines of that code

someClass* myObject = (someClass*)obj;
NSArray *keys = [[[myObject entity] attributesByName] allKeys];
NSDictionary *dict = [myObject dictionaryWithValuesForKeys:keys];
//do something with dict
...

but i cannot figure out how to get an object of the type someClass. I have seen where you can use the NSClassFromString object for an alloc init call like

id someObject = [[className alloc] init];

but that will not work for this situation as i already have an object passed in that I need. any ideas / criticisms? I am really trying to avoid a crap load of if statements for all of my items but it may be the quickest way right now...

like image 247
AtomRiot Avatar asked Mar 27 '26 09:03

AtomRiot


2 Answers

If they are all NSManagedObject subclasses, and all you will be calling on them is valueForKey or other methods that are implemented by NSManagedObject, just cast to NSManagedObject. If I'm missing something, please let me know.

In answer to your comment on the original question - you already have this in your sample:

NSDictionary *myObjectDictionary = [[myObject entity] attributesByName];

This gives you a dictionary containing all the attributes of your managed object, with the attribute name as the key in each case. You then use these keys to call valueForKey on your object.

Perhaps a point I need to make is that it is fine to cast an object as its superclass, if you are not going to call any subclass specific methods on it.

like image 115
jrturton Avatar answered Mar 28 '26 21:03

jrturton


If your sole goal is to convert an object (id) into a dictionary. Since you stated that you know they are all instances of NSManagedObject, you can safely cast it:

- (void)someMethod:(id)object {
  NSManagedObject* managedObject = (NSManagedObject *)object;

Actually, if you want to be curt about it, this could be changed to:

- (void)someMethod:(NSManagedObject *)managedObject {

if you're declaring the method.

You can access the meta data about a managed object by accessing its entity:

  NSEntityDescription *entity = [managedObject entity];

From there, get a description of each property:

  NSDictionary *descriptions = [entity propertiesByName];

This dictionary will have all the keys you want in your dictionary, and an NSAttributeDescription or an NSRelationshipDescription as the value.

The fact that you have written custom classes to provide functionality on these classes is irrelevant! You can access all the data in them regardless of whether they are NSManagedObject instances, or subclasses thereof.

like image 44
bshirley Avatar answered Mar 28 '26 23:03

bshirley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!