Nested Arrays in Objective-C (NSMutableArray)

I am trying to build a nested array: firstly, I create an array of "PlayerItems", which will contain 10 arrays, each of which contains objects of objects corresponding to the inventories of each player in the game. The following error appears on the specified line:

error: void value is not ignored, as it should be

What is void value? If I used [[PlayerItems objectAtIndex:i] addObject:myitem], the program compiles, but it crashes. If I comment on this line, it compiles and starts normally. Thanks for the help!

self.PlayerItems = [[NSMutableArray alloc] initWithCapacity:11];
NSMutableArray *itemarray = [[NSMutableArray alloc] initWithCapacity:60];
item *myitem = [[item alloc] init];
item.kind = 1;
for (int i = 1; i < 10; i++) {
    itemarray = [[NSMutableArray alloc] initWithCapacity:60];
    [PlayerItems addObject:itemarray];
    for (int i2 = 1; i2 < 50; i2++) {
        myitem = [[item alloc] init];
        myitem.kind = 1;
        // The error occurs on the line below:
        ((NSMutableArray *) [[PlayerItems objectAtIndex:i] addObject:myitem]);
    }
}
+3
source share
4 answers

I would do it like this:

self.playerItems = [[NSMutableArray alloc] initWithCapacity:11];

NSMutableArray * itemArray;
Item * anItem;
for (int playerIndex = 1; playerIndex <= 10; playerIndex++)
{
    itemArray = [NSMutableArray arrayWithCapacity:60];
    [playerItems addObject:itemArray];

    for (int itemIndex = 1; itemIndex <= 50; itemIndex++)
    {
        anItem = [[Item alloc] init];
        anItem.kind = 1;
        [itemArray addObject:anItem];
        [anItem release];
    }
}

Cocoa, . , , . .

:

- Player Player :

Player.h

@interface Player : NSObject
{
    NSMutableArray * items;
}
@property (readonly) NSMutableArray * items;
@end

Player.m

#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 <= 50; itemIndex++)
    {
        anItem = [[Item alloc] init];
        anItem.kind = 1;
        [items addObject:anItem];
        [anItem release];
    }

    return self;
}

- (void)dealloc
{
    [items release];
    [super dealloc];
}

@end

NSMutableArray * allPlayers = [[NSMutableArray alloc] initWithCapacity:11];

Player * aPlayer;
for (int playerIndex = 1; playerIndex <= 10; playerIndex++)
{
    aPlayer = [[Player alloc] init];
    [allPlayers addObject:aPlayer];
    [aPlayer release];
}

...

[allPlayers release];
+8

, ...

. , - , eJames, .

, . / :

for (NSArray *items in PlayerItems)
    [[items objectAtIndex:0U] setKind:newKind];

:

for (Player *player in players)
    [player setKind:newKind];

?

, , , , , . , Mac, , Bindings AppleScript ( , ) .

eJames, . Cocoa.

pgb, Cocoa ( C, ) 0. , , , , ; . .

pgb , addObject: - , void, NSMutableArray *. , ? , , , , :

Player *player = [[Player alloc] init];

//Set up name, SPECIAL stats, etc.

for (int kind = 1; kind < 50; ++kind) {
    Item *item = [[Item alloc] init]; //I capitalized your class name for you.
    [item setKind:kind];
    [player addInventoryObject:item]; //Assume "inventory" is the name of the property that holds the player items.
    [item release];
}

[allPlayers addObject:player];
[player release];

: -. , , , , , , , .., . :

for (ItemKind *kind in [self kindsOfItems]) {
    Item *item = [[Item alloc] initWithKind:kind];
    [player addInventoryObject:item];
    [item release];
}

, kindsOfItems , (Mac), (iPhone).

+5

Objective-C , for 1.

for (int i = 0; i < 10; i++)

void, addObject:, void, .

:

[[PlayerItems objectAtIndex:i] addObject:myitem];
+1

addObject:, void:

- (void)addObject:(id)anObject

Then you try to apply void to NSMutableArray *. And you also do not assign casting emptiness.

To add subarray to your array, this should be enough:

[[PlayerItems objectAtIndex:i] addObject:myitem];

BTW: you create only 9 arrays and you add only 49 subarrays.

+1
source

Source: https://habr.com/ru/post/1714613/


All Articles