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.

5
  • 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. Commented Nov 17, 2013 at 19:57
  • 3
    Perhaps try using NSMutableOrderedSet instead Commented Nov 17, 2013 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. Commented Nov 17, 2013 at 20:14
  • i just have to check imageid which is unique. Commented Nov 17, 2013 at 20:16
  • Using a Set would be your best option. This answer may help you Commented Nov 17, 2013 at 20:23

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

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.