开发者

Retrieve properties of "id" type object iphone

开发者 https://www.devze.com 2023-01-05 20:35 出处:网络
I\'m facing some difficulty in retrieving properties of \"id\" type object. This is how I\'m accessing it:

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"];
    开发者_如何学C[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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号