Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would do it as follows:</p> <pre><code>self.playerItems = [[NSMutableArray alloc] initWithCapacity:11]; NSMutableArray * itemArray; Item * anItem; for (int playerIndex = 1; playerIndex &lt;= 10; playerIndex++) { itemArray = [NSMutableArray arrayWithCapacity:60]; [playerItems addObject:itemArray]; for (int itemIndex = 1; itemIndex &lt;= 50; itemIndex++) { anItem = [[Item alloc] init]; anItem.kind = 1; [itemArray addObject:anItem]; [anItem release]; } } </code></pre> <p>As a side note, you should definitely read up on <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html" rel="noreferrer">memory management in Cocoa</a>, since your original code is full of memory leaks. It can be a bit difficult to wrap your head around at first, but once you learn it, it becomes second nature. Well worth the effort.</p> <p><strong>update:</strong></p> <p>A more object-oriented way to do this would be to create a <code>Player</code> class, and have each <code>Player</code> manage its own set of items:</p> <p><strong>Player.h</strong> </p> <pre><code>@interface Player : NSObject { NSMutableArray * items; } @property (readonly) NSMutableArray * items; @end </code></pre> <p><strong>Player.m</strong> </p> <pre><code>#import "Player.h" @implementation Player @synthesize items; - (id)init { if ((self = [super init]) == nil) { return nil; } items = [[NSMutableArray alloc] initWithCapacity:60]; Item * anItem; for (int itemIndex = 1; itemIndex &lt;= 50; itemIndex++) { anItem = [[Item alloc] init]; anItem.kind = 1; [items addObject:anItem]; [anItem release]; } return self; } - (void)dealloc { [items release]; [super dealloc]; } @end </code></pre> <p><strong>Elsewhere</strong> </p> <pre><code>NSMutableArray * allPlayers = [[NSMutableArray alloc] initWithCapacity:11]; Player * aPlayer; for (int playerIndex = 1; playerIndex &lt;= 10; playerIndex++) { aPlayer = [[Player alloc] init]; [allPlayers addObject:aPlayer]; [aPlayer release]; } ... [allPlayers release]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload