0

I have an array of dictionary like below

    self->arrField: (
        {
        fieldname = "Have you ever had any one of the following:";
        fieldvalue =         (
            Diabetes,
            Hypertension
        );
        tagvalue = 0;
    },
        {
        fieldname = "By continuing with this survey, you agree to our terms of use and Parsons Information Governance and Privacy Policies, Procedures and Standards, available on the Corporate Policy Center.";
        fieldvalue = Agree;
        tagvalue = 1;
    },
        {
        fieldname = "Have you tested positive for COVID-19 in the last 10 days?";
        fieldvalue = No;
        tagvalue = 4;
    },
        {
        fieldname = "Do you have any known heart conditions or on any medications that elevate your heart rate";
        fieldvalue = Yes;
        tagvalue = 6;
    },
        {
        fieldname = "Have you received a COVID-19 test in the past 48 hours";
        fieldvalue = Yes;
        tagvalue = 7;
    },
        {
        fieldname = "In the last 48 hours have you experienced any of these symptoms";
        fieldvalue =         (
            cough,
            fever
        );
        tagvalue = 10;
    }
)  

So if the user changes any of the fieldvalue I need to replace that array, I have tried using replaceObjectAtIndex but it will not work out because I need compare the string.
Below is my implementation:

-(void)ChkUnChk:(UIButton*)sender {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIButton *btn=(UIButton *)sender;
        NSString *strValue = btn.accessibilityHint;
        NSString *Str=[NSString stringWithFormat:@"%d",(int)btn.tag];
        int iTag = [[sender.layer valueForKey:@"anyKey"] intValue];
        // new Implementation
        NSMutableDictionary *dictFields = [[NSMutableDictionary alloc] init];
        NSMutableArray *arrFieldvalue = [[NSMutableArray alloc] init];
        [dictFields setObject:[[self->dictCustomFieldGroups valueForKey:@"name"] objectAtIndex:iTag] forKey:@"fieldname"];
        [dictFields setObject:[NSString stringWithFormat:@"%ld",(long)iTag] forKey:@"tagvalue"];
        if ([self isTagIDAvailable:iTag]) {
            arrFieldvalue = [[[self->arrTempCheckboxFields objectAtIndex:self->checkboxIndex] valueForKey:@"fieldvalue"] mutableCopy];
            
            BOOL isTheObjectThere = [arrFieldvalue containsObject: strValue];
            if (isTheObjectThere) {
                NSLog(@"string contains");
                [btn setBackgroundImage:[UIImage systemImageNamed:@"square"] forState:UIControlStateNormal];
                for(id item in arrFieldvalue) {
                    if([item isEqual:strValue]) {
                        [arrFieldvalue removeObject:item];
                        [dictFields setObject:arrFieldvalue forKey:@"fieldvalue"];
                        break;
                    }
                }
            } else {
                NSLog(@"string does not contain!");
                [btn setBackgroundImage:[UIImage systemImageNamed:@"checkmark.square"] forState:UIControlStateNormal];
                [arrFieldvalue addObject:strValue];
                [dictFields setObject:arrFieldvalue forKey:@"fieldvalue"];
            }
            NSInteger count = [self->arrFields count];
            for (NSInteger index = (count - 1); index >= 0; index--) {
                NSArray *p = self->arrFields[index];
                if ([[self->arrFields valueForKey:@"tagvalue"] isEqual:[NSNumber numberWithInt:iTag]]) {
                    [self->arrFields removeObjectAtIndex:index];
                }
            }
            
             [self->arrFields replaceObjectAtIndex:iTag withObject:dictFields];
            [self->arrTempCheckboxFields replaceObjectAtIndex:self->checkboxIndex withObject:dictFields];
        } else {
            [btn setBackgroundImage:[UIImage systemImageNamed:@"checkmark.square"] forState:UIControlStateNormal];
            [arrFieldvalue addObject:strValue];
            [dictFields setObject:arrFieldvalue forKey:@"fieldvalue"];
            [self->arrFields addObject:dictFields];
            [self->arrTempCheckboxFields addObject:dictFields];
        }
        NSLog(@"self->arrField: %@",self->arrFields);
    });
}   
-(BOOL) isTagIDAvailable:(int)tagID {
    BOOL retVal = FALSE;
    BOOL tagIdFound = FALSE;
    if ([arrTagID count] > 0) {
        for (int i = 0; i < [arrTagID count]; i++) {
            if ([[NSNumber numberWithInteger:tagID] isEqualToNumber:[arrTagID objectAtIndex:i]]) {
                tagIdFound = TRUE;
                checkboxIndex = i;
                retVal = TRUE;
                break;
            }
        }
        if (!tagIdFound) {
            [arrTagID addObject:[NSNumber numberWithInteger:tagID]];
            checkboxIndex = (int)[arrTagID count];
            retVal = FALSE;
        }
    } else {
        [arrTagID addObject:[NSNumber numberWithInteger:tagID]];
        retVal = FALSE;
    }
    return retVal;
} 

I have done something from line no 32, like I will remove that array value and add it again.But I am not sure how to do that. If more detail require I can provide.

Thanks in Advance.

6
  • Hold an array of objects, not an array of dictionaries. You will find this much easier to manipulate. Commented Jul 8, 2020 at 7:28
  • @trojanfoe array of objects will be difficult in my scenario. actually I have not put my whole code. Is there any chance to do change something in this code? Commented Jul 8, 2020 at 7:36
  • Why is it difficult? And regardless if it was easy, anyone could program. Commented Jul 8, 2020 at 7:39
  • if you could give me one example of array of objects in this scenario it will be helpful Commented Jul 8, 2020 at 7:55
  • hint: dispatch_async(dispatch_get_main_queue(),^{}); needs to wrap only method calls that change the btn otherwise you make the whole code run in main thread. Commented Jul 8, 2020 at 12:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.