tags:

views:

44

answers:

1

Hi all,

I'm facing some difficulty in retrieving properties of "id" type object. This is how I'm accessing it:

I'm doing following to assign an object to id type object from a generic array containing different types of objects and calling method "savedata" to which I'm passing the object as well as its type:

for(id objInArray in genericArray){
    NSString *objType = [objInArray valueForKey:@"type"];
    [objInArray retain];
    [self saveData:objInArray :objType];
}

In savedata method I'm writing following code to retrieve the properties of id object:

-(void)saveData:(id)object :(NSString *)objectType
{
self.managedObjectContext = appDelegate.managedObjectContext;

if([objectType isEqualToString:@"event"])
{
    Event * newEvent = (Event *)[NSEntityDescription 
                                 insertNewObjectForEntityForName:@"Event" 
                                 inManagedObjectContext:self.managedObjectContext];


    [newEvent setEletitle:[NSString stringWithFormat:@"%@", [object valueForKey:@"eletitle"]]]; 

    [self saveAction];
}

But the object "object" containing the values fails to assign them to object newEvent. I also tried to retrive this value in a string object like this:

NSString *eletit = [object valueForKey:@"eletitle"];
    [eletit retain];

But eletit is also invalid at the end of this transaction.

Can anybody please help? This' really urgent.

Thanx in advance.

A: 

I don't have you answer unfortunately but I have few comments on your code. Are you sure it's normal you array contain so generics object? It's strange because all your object contained in your array need to respond to "type" or "eletitle" messages, so I guess objInArray is less generic than just "id".

Second, it's not recommended to have selector like saveData::, in Objective-C it's usual and recommended to name the arguments, it's more understandable.

Greensource
1. The objects in my array are objects of different types with only a couple of attributes similar [like id, type]. Basically I'm trying to fetch the objects as a whole into the id object, so that's normal. 2. You are right in saying that I should name my arguments. I'll surely mend it.
neha
Ok, but all your object must respond to "type" and "eletitl" so you must create a Protocol, and all the object in your array must respect this protocol.I know that not resolve your issue unfortunately, but it's best practice.Regards
Greensource