Andrew
Andrew

Reputation: 16051

Arrays within arrays - trying to figure out what i need to do?

This is difficult to work out and i'm having trouble getting my head around it. Maybe someone could help?

So i have a series of 6 UIColors which I want grouped together, so i could refer to them as:

[palette objectAtIndex:3]; and get UIColor 3.

But on top of this, I want to have a series of these colour palettes. So i could go:

[allPalettes objectAtIndex:3]; and get palette 3. How would i go about putting array inside of an array like this?

Upvotes: 0

Views: 46

Answers (2)

NWCoder
NWCoder

Reputation: 5266

// Lets assume you have methods to created the series of arrays of UIColors 

NSArray *brownPalette = [self createBrownPalette];
NSArray *brightPalette = [self createBrightPalette];
NSArray *orangePalette = [self createOrangePalette];
NSArray *grayPalette = [self createGrayPalette];
NSArray *earthPalette = [self createEarthPalette];

NSArray *palettes = [NSArray arrayWithObjects:
      brownPalette,
      brightPalette,
      orangePalette,
      grayPalette,
      earthPalette, nil];

Upvotes: 0

Max
Max

Reputation: 16709

If you need to store array of arrays then:

NSArray* pallets = ...//get that array
NSArray* pallet_colors = [pallets objectAtIndex: 3];
UIColor* color = [pallet_colors objectAtIndex: 3];

Or to make it shorter:

NSArray* pallets = ...//get that array
UIColor* color = [[pallets objectAtIndex: 3] objectAtIndex: 3];

However I'd recommend you to not to store all pallets and colors in plain array. You should create custom containers (like MyPallet class) with appropriate accessor methods for getting the color by index/name etc.

Upvotes: 1

Related Questions