0

I'm adding imageDataObject in imagesArray.

ImageData *imageDataObject = [[ImageData alloc]initWithImageId:[[imageListArray objectAtIndex:indexPath.row]imageId]
                                                     imageName:[[imageListArray objectAtIndex:indexPath.row] imageName]
                                                         image:[UIImage imageWithData:imageData]];
[imagesArray addObject:imageDataObject];

I want to avoid adding imageDataObject if it already exists in imagesArray. imageDataObject is duplicate if imageId is already in the imagesArray object. imageId is string value coming from URL.

I tried containsObject but it's not working.

I know there are similar questions but nothing works for me.

please help.

user2769614
  • 247
  • 3
  • 6
  • 23
  • 2
    What makes two `imageDataObject` the same? Just because two `imageDataObject` hold the same image or imageName doesn't make them the same (in terms of `containsObject`). They just have the same contents. – nhgrif Nov 17 '13 at 19:57
  • 3
    Perhaps try using [`NSMutableOrderedSet`](https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSMutableOrderedSet_Class/Reference/Reference.html) instead – Kevin Nov 17 '13 at 19:58
  • There is no practical way to tell that you have two distinct but identical images. If you want to avoid dupes you need to keep track of the sources of the images and eliminate images with identical sources. – Hot Licks Nov 17 '13 at 20:14
  • i just have to check imageid which is unique. – user2769614 Nov 17 '13 at 20:16
  • Using a `Set` would be your best option. [This answer](http://stackoverflow.com/questions/10586218/objective-c-nsmutableset-unique-object-property/10586479#10586479) may help you – Alladinian Nov 17 '13 at 20:23

2 Answers2

1

You need to implement -isEqual: and -hash for your ImageData class. Once you do that correctly, then -containsObject: or a set (e.g. NSMutableOrderedSet) will be able to work correctly.

You don't explain what type an imageId is. If it's an object type, then you can piggy-back on its definition of equality. If it's an integer type, you can either do something simple like using the integer value as the hash, or you can implement it in terms of NSNumbers equality and hash implementations.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
1

Many ways, here are a selection:

  1. It looks like ImageData is your own type. NSArray's containsObject: determines if a matching object exists by calling isEqual:. So define isEqual: and hash (you must implement both) on ImageData which just compares for equality based on your imageID (whatever the type of that is).
  2. If you cannot define isEqual: and hash as they already exist and are not based on imagedID then you can use NSArray's indexOfObjectPassingTest: method. This takes a block which is called to test each element of the array in turn and returns the index of the first element which matches, or NSNotFound if no such element exists. Have the block test for your imageID and if the method returns NSNotFound you know there is no duplicate and the new ImageData should be added.
  3. Store the ImageData objects in an NSMutableDictionary using your imageID as the key (if imageID is a primitive type then wrap it as an object to use it as a key, e.g. wrap int as an NSNumber - using modern syntax 5 is just @5 as an NSNumber). You can easily test for key existence (a lookup returns nil), or just add new objects without checking as if there is a repeat the new value will just replace the old one.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86