3

I have an NSMutableArray where objects can be added to. The objects added are SUPDataValue objects containing a bunch of SUP data.

In an other view, I take this object array and divide it into an NSMutableArray containing an NSMutableArray for every section in the tableview. When add another SUPDataValue object to my initial Object array, and I switch back to my table view, I want to re-read all the object from the object array and see if those objects exist in my layered array.

I am using the "objectExists" and it works great... however... if I add the same object twice to my object array, it will always assume it exists in a layer.

This is my code:

- (void)setInitialComponents:(NSMutableArray*)components
{
    if (self.componentLayer)
    {
        for (SUPDataValueList *val in components)
        {
            BOOL found = NO;

            for (NSMutableArray *layer in self.componentLayer)
            {
                if ([layer containsObject:val])
                {
                    found = YES;
                }
            }

            if (!found)
            {
                [[self.componentLayer objectAtIndex:0] addObject:val];
            }
        }
    }
    else {
        self.componentLayer = [NSMutableArray array];

        // Add the no-layer layer (section 0)
        [self.componentLayer addObject:[NSMutableArray array]];

        if (self.addMode)
        {
            [[self.componentLayer objectAtIndex:0] addObjectsFromArray:components];
        }
        else {
            for (SUPDataValueList * val in components)
            {
                int layer = [[NSString stringWithFormat:@"%@", [val item:38]] intValue];

                if (self.componentLayer.count < layer)
                {
                    [self.componentLayer insertObject:[NSMutableArray array] atIndex:layer-1];
                }

                [[self.componentLayer objectAtIndex:layer-1] addObject:val];
            }
        }
    }

    [self.tableView reloadData];
}

As you might have guessed, my problem is here:

if ([layer containsObject:val])
{
    found = YES;
}

I would like to check if an unique object exist in that array (using memory allocation ID or something?) How do I do that?

4
  • Why not just use NSSet? Commented Nov 6, 2013 at 14:36
  • Because you cannot add two of the same object to an NSSet if I am not misstaken. I tried an NSSet and I only got 1 SUPDataValue object, not several of the same. Commented Nov 6, 2013 at 14:38
  • NSCountedSet might fit your bill, then Commented Nov 6, 2013 at 14:41
  • Ok, I'll have a look. Commented Nov 6, 2013 at 14:42

2 Answers 2

4

The containsObject method will invoke isEqual: on the underlying objects being compared.

Unless you implement isEqual: in the SUPDataValueList object, it will simply do a pointer comparison which is the default behavior of isEqual in NSObject.

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

2 Comments

Ok, so what you are saying is that I must be adding the same object several times to my component array, correct?
I found the solution. I was adding the same memory object over and over again, and since genially SUP doesn't support copy on SUPDataValueList I had to write my own solution around it. So, indirect your answer is correct. It works exactly as it should containsObject that is.
0

You're looking for -[NSArray indexOfObjectIdenticalTo:], which uses the objects' addresses to determine a match.

found = [layer indexOfObjectIdenticalTo:val] != NSNotFound;

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.