0

I have a Stack to fill with an array of views.

_countViewArray = [[NSArray alloc] init];
_countViewArray = @[self.a.view,self.b.view,self.c.view];
_stackView = [NSStackView stackViewWithViews:_countViewArray];

It's work well. If I want replace this array with a mutable array how can do it?

I tried this code for a "dynamic" stack view and finally convert mutable array in simple array but doesn't work:

_mutableCountViewArray = [[NSMutableArray alloc] init];

[_mutableCountViewArray addObject:@[self.a.view]];
if (caseCondition){
   [_mutableCountViewArray addObject:@[self.b.view]];
}
[_mutableCountViewArray addObject:@[self.c.view]];

_countViewArray = [_mutableCountViewArray copy];
_stackView = [NSStackView stackViewWithViews:_countViewArray];

in consolle if I print mutable array I have:

(
    (
    "<NSView: 0x600000121ea0>"
),
    (
    "<NSView: 0x600000120780>"
,
    (
    "<NSView: 0x6000001235a0>"
)
)

How can I solve?

Alladinian
  • 34,483
  • 6
  • 89
  • 91
Joannes
  • 2,569
  • 3
  • 17
  • 39

1 Answers1

1

The problem is that you are adding arrays (containing a single view) instead of views...

Remember, @[x] is a literal expression defining an NSArray containing x


So a line like this:

[_mutableCountViewArray addObject:@[self.a.view]];

should become:

[_mutableCountViewArray addObject:self.a.view];

(of course, this applies to every object you add in the next few lines...)


Also, as a sidenote:

_countViewArray = [[NSArray alloc] init];

in your first snippet is redundant since you reassign a value in the next line...

Alladinian
  • 34,483
  • 6
  • 89
  • 91
  • Hey, thanks! I do not understand the sense of redundant. The first block of code is without mutable array, the second alloc of array is understood... – Joannes Mar 07 '17 at 15:37
  • @alfioal I mean that you can safely delete the line `_countViewArray = [[NSArray alloc] init];` (in the case you use the first snippet) since you assign a different value to `_countViewArray` again in the next line... I hope that this makes sense :) – Alladinian Mar 07 '17 at 15:39
  • Ah, ok! I understand! – Joannes Mar 07 '17 at 15:58