简体   繁体   中英

NSMutableArray in NSMutableArray as a single object

I am trying to add NSMutableArray in another NSMutableArray. But what I am trying to do is nested arrays.

My current code is:

  NSMutableArray *array1 = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
  NSMutableArray *array2 =[[NSMutableArray alloc] init];
  [array2 addObject:array1];

This code is adding 4 objects in array2 but I want it to add array1 as single object.

Edit: This code is working I know but in my case in XCode something is wrong with initializing and it is adding 4 objects. I still could not figure it out. So this piece of code is working properly. So the problem was about initialization in a for loop.

I copy/pasted your code, and it adds one object to array2, not four.

Printing description of array2:
<__NSArrayM 0xc46c7b0>(  <-- THIS ARRAY HAS 1 OBJECT
    <__NSArrayM 0xc488770>( <-- THIS ARRAY HAS 4 OBJECTS
        Red,
        Green,
        Blue,
        Yellow
    )
)

You may be are getting confused by the fact that printing the description, prints the contents of the inner array also.

尝试:

NSMutableArray *array2 =[[NSMutableArray alloc] initWithArray:array1];

我正在使用它及其工作

NSArray *array1 = @[array2, array3, ...];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM