0

I receive data for an object person in sets of 5. Let's say name,age,gender,email,number. I did following to add the strings to NSobject:

DataObject *data=[DataObject new];
data.name=@"name";
data.age=@"age";
data.email=@"email";
//here i want to check for duplicates
[personArray addObject:data];

However, I want to check if the personArray is having the duplicate NSObjects or not.

I tried this,but it didnt work:

if(![personArray containsObject:data]){
      //add data
}

Edit: Actually, this is what I am trying to do:

I am getting the JSON repsonse and I am adding the properties into array. Before I used to get only one property,in that case, I did the following to eliminate the duplicates:

[JSON[@"person"] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
       if (![obj[@"email"] isEqual:[NSNull null]] && ![personArray containsObject:obj[@"email"]]  ) {

               [personArray addObject:obj[@"email"]];

        }

    }];

Later I got 5 properties for person, so I thought instead of adding them all to the array, I used NSObject class to tie the properties together and add one person to the array.

[JSON[@"person"] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
           if (![obj[@"email"] isEqual:[NSNull null]] && ![personArray containsObject:obj[@"email"]]  ) { //how to check for the duplicates here? 

                 DataObject *data=[DataObject new];
                 data.name=@"name";
                 data.age=@"age";
                 data.email=@"email";
                [personArray addObject:data];

            }

        }];
ZeeroCool77
  • 191
  • 1
  • 13
  • possible duplicate of [The best way to remove duplicate values from NSMutableArray in Objective-C?](http://stackoverflow.com/questions/1025674/the-best-way-to-remove-duplicate-values-from-nsmutablearray-in-objective-c) – Simon Degn Jul 13 '15 at 17:57

2 Answers2

1

You need to implements isEqual for the DataObject class. Then [personArray containsObject:data] should work.

For details see:

zaph
  • 111,848
  • 21
  • 189
  • 228
1

If you do this:

DataObject *data = [DataObject new];

You have just created a new instance of data. No other object inside the personArray can be equal to that new instance.

I assume you're actually trying to check to see if there is a data object that contains the same properties as other data objects in the personArray. There's a number of ways you could do this (I like Zaph's answer, it's clean), but for simplicity...

DataObject *data=[DataObject new];
data.name=@"name";
data.age=@"age";
data.email=@"email";    

BOOL contains = NO;
for (DataObject *object in personArray) {
    if ([object.name isEqualToString:data.name] && [object.age isEqualToString:data.age] && [object.email isEqualToString:data.email]) {
        contains = YES;
        break;
    }
}

if (!contains) {
    [personArray addObject:data];
}
Frankie
  • 11,508
  • 5
  • 53
  • 60
  • Thank you for providing the additional information, but I feel like both the available answers still apply. – Frankie Jul 13 '15 at 18:33
  • This should not be the accepted answer. The correct way to implement this is how @zaph has shown in his answer. – Paul.s Jul 13 '15 at 21:55