Domanda

I have a weird problem.

M_DrugListRow have these properties:NAME,SEGMENT,ATTRIBUTE,COLORCODE,ID,DESCRIPTION

dict have these drug_name,segment,attribute,colorcode,..

M_DrugListRow *m= [[M_DrugListRow alloc]initDrugRow:[arr_for_picker objectAtIndex:row] val:0]
[dict setValue:m.NAME forKeyPath:@"drug_name"];

After this code dict has M_DrugListRow's keys(NAME,SEGMENT,ATTRIBUTE,COLORCODE,DESCRIPTION), not his own keys(name,segment,attribute,colorcode,drug_name) So it gives error in my code.

If I change it into this code, it works:

[dict setValue:[[arr_for_picker objectAtIndex:row]objectForKey:@NAME"] forKeyPath:@"drug_name"];

I use the first type too much. But it isn't efficiant. Why is that happening and how can I solve this?

Code: M_DrugListRow.h

             @interface M_DrugListRow : M_Drug
-(id)initDrugRow:(M_Drug*)drugModel val:(NSInteger)val
{
    self=[super init];
    
    @try {
        
        self=[self initDrug:[NSDictionary dictionaryWithPropertiesOfObject:drugModel]];
        liquid =[[NSMutableArray alloc]init];
        method =[[NSMutableArray alloc]init];
        
        if(val){
            liquid= [DataAccessLiquid drugliquid:[VC_Login getGuid] drug_id:self.ID];
            method= [DataAccessMethod:[VC_Login getGuid] drug_id:self.ID];
            
        }
        
    }
    @catch (NSException *exception) {
        
    }
    @finally {
        
    }
    return self;
}

M_Drug.h

   -(id)initDrug:(NSDictionary*)par
    {
        self=[super init];
        dict =[[NSDictionary alloc]initWithDictionary:par];
        @try {
            ATTRIBUTE1= [par valueForKey:@"ATTRIBUTE1"];
            NAME= [par valueForKey:@"NAME"];
            COLORCODE= [[par valueForKey:@"COLORCODE"]boolValue];
            DESCRIPTION= [par valueForKey:@"DESCRIPTION"];
            SEGMENT= [par valueForKey:@“SEGMENT”];
            ID= [[par valueForKey:@"ID"]integerValue];
            
        }
        @catch (NSException *exception) {
            
        }
        @finally {
            
        }
        
        return self;
    }
È stato utile?

Soluzione

You need to use a NSMutableDictionary instead of NSDictionary. This is a subclass of NSDictionary and is editable.

Have a look at this answer for a good explanation.

An example:

NSMutableDictionary *mdict = [[NSMutableDicionary alloc]init];
[mdict setObject:@"Hello World!" forKey:@"whateverKey"]

Read up on the class reference here.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top