NSCollectionView: can the same object not be in the array more than once or is this a bug?

I might be doing it all wrong, but I thought I was on the right track until I hit that little grip. I was basically assembling a toy using NSCollectionView and trying to figure out how to hook everything up using IB. I have a button that will add a couple of lines to the NSArrayController:

alt text

The first time I click this button, my rows are displayed as a collection as expected:

alt text

The second time I click the button, the views scroll down and the room is created, but no items seem to be added. I just see empty space:

alt text

The button is implemented as follows (the controller is a pointer to an NSArrayController that I added to IB):

- (IBAction)addStuff:(id)control
{
    [controller addObjects:[NSArray arrayWithObjects:@"String 1",@"String 2",@"String 3",nil]];
}

      

I'm not sure what I am doing wrong. Rather than trying to explain all the / binds / etc connections if you need more information, I would appreciate it if you could have a quick look at the toy project .

UPDATE: After additional experimentation as suggested by James Williams, it seems that the problem is related to having multiple objects with the same memory address in the array. This confuses either NSArrayController or NSCollectionView (not sure which). Changing my addStuff: this resulted in the behavior I originally expected:

[controller addObjects:[NSArray arrayWithObjects:[NSMutableString stringWithString:@"String 1"],[NSMutableString stringWithString:@"String 2"],[NSMutableString stringWithString:@"String 3"],nil]];

      

So the question is probably, is this a bug that I should report to Apple or if this is intended / documented behavior and I just missed it?

+2


source to share


3 answers


Assigning the same immutable string to an address by design. This is performance optimization.

Another way to achieve your goal:

NSString* param1 = [NSString stringWithFormat:@"string1"];
NSString* param2 = [NSString stringWithFormat:@"string2"];
NSString* param3 = [NSString stringWithFormat:@"string3"];

[controller addObjects:[NSArray arrayWithObjects:param1,param2,param3,nil]];

[param1 release];
[param2 release];
[param3 release];

      



edit: Just using stringWithFormat is enough, it turns out

[controller addObjects:[NSArray arrayWithObjects:[NSString stringWithFormat:@"string1"],
                                                 [NSString stringWithFormat:@"string2"],
                                                 [NSString stringWithFormat:@"string3"],nil]];

      

+2


source


[Edit: doing some games and it definitely seems like a "duplicate" issue. You might want to revisit this question for better answers. This is really weird. I would like to receive an answer myself.]

It's better to deal with this:


if([[controller arrangedObjects] count] == 0)
   [controller addObjects:[NSArray arrayWithObjects:@"String 1",@"String 2",@"String 3",nil]];
else 
   [controller addObjects:[NSArray arrayWithObjects:@"String 4", @"String 5", @"String 6", nil]];

      

which leads me to believe it has something to do with @ "String 1" etc., being the same object twice in an array. I expected this to work.



So I don't really understand what's going on. But bypass is not to put identical things there. Not a good job, huh?

I would expect


[controller addObjects:[NSArray arrayWithObjects:
                            [[NSString alloc] initWithString:@"String 1"],
                            [[NSString alloc] initWithString:@"String 2"],
                            [[NSString alloc] initWithString:@"String 3"],
                            nil]];

      

work. But this is not the case. Which also confuses me. Sometimes the NSCollectionView is very strange.

+2


source


I would not call this type of constraint a bug. A data-driven view typically uses content objects as keys. For example, in NSOutlineView, you have two methods:

– itemAtRow:
– rowForItem:

      

Where element is an arbitrary object that is passed through bindings or through the data source mechanism. If the objects in the outline view are not unique, how is NSOutlineView supposed to reliably implement rowForItem :? If the same object appeared N times in outline view, there would be N lines for it.

The NSCollectionView does not seem to have an interface that excludes multiple instances of the same content object, but I think this expectation is typical of data driven views.

You must use unique content objects.

+2


source







All Articles