• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: array problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: array problem


  • Subject: Re: array problem
  • From: Dave Rosborough <email@hidden>
  • Date: Thu, 11 Mar 2004 07:07:11 -0800

Basically, the problem is that you need to declare your "temp" array inside the loop - that way you'll make a new one each time through the loop. You don't really want "temp" to be an instance variable, either! (I'm assuming both arrays are declared in your header file, right?)

Try this:

- (void)makeMatrix
{
int i, j;
matrixArray = [[NSMutableArray array] retain];

for(i=0; i<x; i++) // x is an int between 3 and 5
{
NSMutableArray *temp = [NSMutableArray array]; // this will create a new array for each time through the loop, which is then
// placed in matrixArray. Also, notice that I'm not "retaining" it - matrixArray
// will do that for me when I put temp into it.
for(j=0; j<x; j++) // x is an int between 3 and 5
{
[temp addObject:[NSNumber numberWithInt:0]];
}
[matrixArray addObject:temp];
}
}


On 11-Mar-04, at 4:47 AM, Sascha Kuehn wrote:

hi,

i have a problem with creating an array of arrays
this is my code:

----------------------------------------------------------------------- -
-
- (void)makeMatrix
{
int i, j;
matrixArray = [[NSMutableArray array] retain];
temp = [[NSMutableArray array] retain];

for(i=0; i<x; i++) // x is an int between 3 and 5
{
for(j=0; j<x; j++) // x is an int between 3 and 5
{
[temp addObject:[NSNumber numberWithInt:0]];
}
[matrixArray addObject:temp];
}
[temp release];
}
----------------------------------------------------------------------- -
-

what i need is an array of arrays with numbers.
in case of x = 3 like this: {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}

in case of x = 4 like this: {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0},
{0, 0, 0, 0}}

in case of x = 5 like this: {{0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0,
0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}}

and so on.

but my method don't do that :(

sascha
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >array problem (From: Sascha Kuehn <email@hidden>)

  • Prev by Date: NSMatrix doesn't highlight correctly
  • Next by Date: Re: Wisdom of overriding isEqual:
  • Previous by thread: array problem
  • Next by thread: Re: array problem
  • Index(es):
    • Date
    • Thread