ios - How to check the duplicate NSObjects in NSMutableArray -


i receive data object person in sets of 5. let's name,age,gender,email,number. did following add strings nsobject:

dataobject *data=[dataobject new]; data.name=@"name"; data.age=@"age"; data.email=@"email"; //here want check duplicates [personarray addobject:data]; 

however, want check if personarray having duplicate nsobjects or not.

i tried this,but didnt work:

if(![personarray containsobject:data]){       //add data } 

edit: actually, trying do:

i getting json repsonse , adding properties array. before used 1 property,in case, did following eliminate 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 got 5 properties person, thought instead of adding them array, used nsobject class tie properties , add 1 person array.

[json[@"person"] enumerateobjectsusingblock:^(id obj, nsuinteger idx, bool *stop) {            if (![obj[@"email"] isequal:[nsnull null]] && ![personarray containsobject:obj[@"email"]]  ) { //how check duplicates here?                    dataobject *data=[dataobject new];                  data.name=@"name";                  data.age=@"age";                  data.email=@"email";                 [personarray addobject:data];              }          }]; 

if this:

dataobject *data = [dataobject new]; 

you have created new instance of data. no other object inside personarray can equal new instance.

i assume you're trying check see if there data object contains same properties other data objects in personarray. there's number of ways (i zaph's answer, it's clean), simplicity...

dataobject *data=[dataobject new]; data.name=@"name"; data.age=@"age"; data.email=@"email";      bool contains = no; (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]; } 

Comments