0

I am getting data from Dictionary. It works well and stores data in NSMutableArray I want that before adding object into need to make sure that Array does not contain same object with Same Name and Type. Please see below.

Before inserting object we should check that it does not contain object with Type and Name if contains no need to insert.

NSArray *resultDic = [result1 objectForKey:@"results"];

for (int i = 0; i<[resultDic count]; i++) {
    id item = [resultDic objectAtIndex:i];

    NSDictionary *jsonDict = (NSDictionary *) item;
    GetData  *theObject =[[GetData alloc] init];

    NSString*error = [jsonDict valueForKey:@"error"];
    if(![error isEqualToString:@"No Record Found."])
    {



        [theObject setVaccineID:[jsonDict valueForKey:@"ID"]];
        [theObject setVaccineName:[jsonDict valueForKey:@"Name"]];
        [theObject setVaccinationType:[jsonDict valueForKey:@"Type"]];
        [theObject setVaccineType:[jsonDict valueForKey:@"VType"]];
        [theObject setFarmName:[jsonDict valueForKey:@"FName"]];
        [theObject setDay:[jsonDict valueForKey:@"Day"]];
        [theObject setAddedDateTime:[jsonDict valueForKey:@"DateTime"]];



        [appDelegate.dataArray addObject:theObject];



    }
}
2

1 Answer 1

1

A general purpose solution is to teach your GetData object how to compare itself to others. If they can be compared, then it will be easy to determine if a match is in any collection (and you might want to compare them in other contexts, too). Do this by implementing isEqual:. That might look something like this:

// in GetData.m
- (BOOL)isEqual:(id)object {
    if ([object isKindOfClass:[GetData self]]) {
        // assuming that the object is fully characterized by it's ID
        return [self.vaccineId isEqual:((GetData *)object).vaccineId];
    }
    else {
        return NO;
    }
}

// have the hash value operate on the same characteristics as isEqual
- (NSUInteger)hash {
    return [self.vaccineId hash];
}

With that done, you can take advantage of NSArray's containsObject:.

// ...
if(![appDelegate.dataArray containsObject:theObject] && ![error isEqualToString:@"No Record Found."])
// ...
Sign up to request clarification or add additional context in comments.

Comments

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.