Objective-C中的嵌套数组(NSMutableArray) [英] Nested arrays in Objective-C ( NSMutableArray )

查看:162
本文介绍了Objective-C中的嵌套数组(NSMutableArray)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个嵌套数组:首先,我创建一个PlayerItems数组,它将包含10个数组,每个数组包含项目对象,对应于游戏中每个玩家的清单。在指示的行,我得到以下错误:


错误:void valued不会被忽略,因为它应该是


这里的void值是什么?如果我使用 [[PlayerItems objectAtIndex:i] addObject:myitem] ,程序会编译但崩溃。如果我评论该行,它编译并运行确定。谢谢您的帮助!

  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;
//错误发生在下面的行:
((NSMutableArray *)[[PlayerItems objectAtIndex:i] addObject:myitem]);
}
}


解决方案

I将执行如下操作:

  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 管理自己的项目: p>

Player.h

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

Player.m b
$ b

  #importPlayer.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

strong>

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

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

...

[allPlayers release];


I'm trying to build a nested array: First, I make a "PlayerItems" array which will contain 10 arrays, each containing item objects, corresponding to the inventories of each player in the game. On the indicated line, I get the following error:

error: void valued not ignored as it ought to be

What is the void value here? If I used [[PlayerItems objectAtIndex:i] addObject:myitem] instead, the program compiles but crashes. If I comment that line out, it compiles and runs OK. Thank you for your 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]);
    }
}

解决方案

I would do it as follows:

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];
    }
}

As a side note, you should definitely read up on memory management in Cocoa, 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.

update:

A more object-oriented way to do this would be to create a Player class, and have each Player manage its own set of items:

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

Elsewhere

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];

这篇关于Objective-C中的嵌套数组(NSMutableArray)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆