Search code examples
objective-cxcodeweb-servicesnullself

Values in self object are being changed to null objective c


if([elementName isEqualToString:@"EmpWithNameAndID"]){
    [emps addObject:empObject];
    NSLog(@"count of emps is : %d",emps.count);

    self.managedObjectContext = appDelegate.managedObjectContext;

    EmpWithName *empWithNameObj = [NSEntityDescription insertNewObjectForEntityForName:@"EmpWithName"
                                                                inManagedObjectContext:self.managedObjectContext];
    if(!self.empObject){
        NSLog(@"Not nil");}
    else {
        NSLog(@"Yo Nil");
    }

    empWithNameObj.empID =self.empObject.empID;//@"123"
    empWithNameObj.empName =self.empObject.empName;//@"ABC"

    NSLog(@"emp object id is: %@",empWithNameObj.empID);
    NSLog(@"emp name is %@",empWithNameObj.empName);


    if(self.managedObjectContext == nil)
    { NSLog(@"S MOC is nil");}
    else {
        NSLog(@"S MOC is Not NIL");
    }

    NSError *error;
    if(![self.managedObjectContext save:&error]){
        NSLog(@"Failed to save the object");
    }
    else {

        NSLog(@"saved object");
    }
    if(error == nil)
    {
        NSLog(@"Good saved");
    }
    else {
        NSLog(@"something went wrong while saving");
    }
}
else {

    if([elementName isEqualToString:@"empName"] || [elementName isEqualToString:@"EmpID"]){

        NSLog(@"Element name is: %@ and its value is : %@ ",elementName,currentElementValue);

        if(!empObject)
        {
            NSLog(@"just empobject not nil");
        }
        else {
            NSLog(@"its nil.empobject");
        }


        if(!self.empObject){
            NSLog(@"Not nil");}
        else {
            NSLog(@"Yo Nil");
        }

        [self.empObject setValue:currentElementValue forKey:elementName];

            if([elementName isEqualToString:@"empName"]){
            NSLog(@"empName stored is %@", self.empObject.empName);
        }
        if([elementName isEqualToString:@"EmpID"]){
            NSLog(@"EmpID value stored is %@",self.empObject.empID);
        }
        currentElementValue = nil;
    }
}

So all the above code is in didEndElement of NSXMLParser. In else part at the end of the code just before i set currentElementValue as nil I have two if conditions.So when the elementName is empName it logs this to the output "empName stored is (null)". But before that i have this log statement "Element name is: empName and its value is :

ABC". 

So my question is if it knows the element name and its value why is it not saving its values. Why are they logging as nulls? Please help. If more information is needed please ask. Thanks


Solution

  • Well i figured out a solution. I dont know if it is correct or not but i got what i wanted. What i did was in the else block instead of saving to self.empObject i made use of a NSMutabledictionary i created in initParser and added the values to them. Then in the if block used that dictionary to retrieve the values by key-name (because i already know the key-names) and assigned them to empNameWithObj and did removeAllObjects on dictionary. Remaining procedure is same. It worked.