从NSMutableArray检索的数据与我保存到的数据不同

时间:2015-05-03 00:06:15

标签: objective-c nsmutablearray

为什么从NSMutableArray中检索的单元格的值与我最初存储在其中的单元格的值不同?

我有一个NSMutableArray类型的属性,我用它来保存一个带有以下代码的整数矩阵

tiles = [[NSMutableArray alloc] initWithObjects:
         @1, @1, @1, @1, @1, @1,
         @1, @0, @0, @0, @0, @1,
         @1 ,@0, @0, @0, @0, @1,
         @1 ,@0, @0, @0, @0, @1,
         @1 ,@0, @0, @0, @0, @1,
         @1, @1, @1, @1, @1, @1,nil];


tileManager = [[TileManager alloc] init]; 
[tileManager CreateTilesMapRow:tilesMatrixWidth withColumns:tilesMatrixHeight byFlatDataArray:tiles];

这里是检索部分,它始终从NSMutableArray返回@ 1,这是一个" iso_wall"

-(void) placeAllTilesIso
{
    NSString *imageName;
    printf("Width , Height : %li , %li \n",(long)tilesMatrixWidth (long)tilesMatrixHeight);
    for (NSInteger i = 0;i<tilesMatrixWidth;i++)
   {
        for(NSInteger j = 0;j<tilesMatrixHeight;j++)
        {
            imageName = [tileManager getSpriteNameForRow:i andCol:j];
//HERE IS THE WRONG RESULT! Always says iso_wall! but It shouldn't if you take a look at tiles which includes both @0 and @1, where did the @0 went?
            NSLog(@"Sprite Got!  image name : %@ \n",imageName);
        }
    }
}

我还定义了一个名为TileManager.m和TileManager.h的类和一个标题,我计划将来扩展它们,这是他们的代码:

这是TileManager.h

#import <UIKit/UIKit.h>
#import <math.h>
@interface TileManager : NSObject
@property (strong,nonatomic) NSMutableArray *tilesMatrix;
- (void) CreateTilesMapRow:(NSInteger) row withColumns:(NSInteger) col byFlatDataArray:(NSMutableArray*) flatArray;
- (NSString *) getSpriteNameForRow:(NSInteger) row andCol:(NSInteger) col;
@end

这是TileManager.m

#import "tileManager.h"
@implementation TileManager

//NSMutableArray *tilesMatrix;

- (void) CreateTilesMapRow:(NSInteger) row withColumns:(NSInteger) col byFlatDataArray:(NSMutableArray *) flatArray
    {

    _tilesMatrix = [[NSMutableArray alloc] initWithCapacity:row];
    NSMutableArray *innerTmp = [[NSMutableArray alloc] initWithCapacity:col];
    for (int i = 0;i < col;i++) [innerTmp addObject:@0];
    for (int i = 0;i < row;i++) [_tilesMatrix addObject:innerTmp];

   for (NSInteger rowIndex=0; rowIndex < row;rowIndex++)   
     {
        for (NSInteger colIndex=0; colIndex < col; colIndex++)
        {
            _tilesMatrix[rowIndex][colIndex] =(NSString*) flatArray[rowIndex*col+colIndex];
        //READ ME
        // This show everything is ok and the _tileMatrix contains both 0 and 1
            NSLog(@"Create : row : %li    col : %li  val : %@",(long)rowIndex,(long)colIndex,_tilesMatrix[rowIndex][colIndex]);
            }
        }
    }
}
-(NSString *) getSpriteNameForRow:(NSInteger) row andCol:(NSInteger) col 
{
    //README
    // This always writes 1 which is incorrect
    NSLog(@"GET : row : %li    col : %li  val : %@",(long)row,(long)col,_tilesMatrix[row][col]);
    if ([_tilesMatrix[row][col] isEqual:@1])
        return (NSString *) @"iso_wall";
    else
        return (NSString *) @"iso_ground";
}

为什么在Objective C中使用数组是如此困难!!!

2 个答案:

答案 0 :(得分:1)

for (int i = 0;i < col;i++) [innerTmp addObject:@0];

使用innerTmp零填充col ...

for (int i = 0;i < row;i++) [_tilesMatrix addObject:innerTmp];

innerTmp row次添加到_tilesMatrix ...

您已经构建了一个矩阵,其中每一行都是对相同对象的引用 - innerTmp

也许试试:

[_tilesMatrix addObject:[innerTmp mutableCopy]];

所以每一行都是不同的。

HTH

答案 1 :(得分:0)

这两行是否可能导致问题:

for (int i = 0;i < col;i++) [innerTmp addObject:@0];
for (int i = 0;i < row;i++) [_tilesMatrix addObject:innerTmp];

我现在不能运行代码但是其他一切看起来都很稳固,除了这些。所有行都添加了相同的innerTmp。同样的记忆。而不是:

for (int i = 0;i < row;i++) {
    NSMutableArray * innerTmp = [[NSMutableArray alloc] initWithCapacity:col];
    for (int j = 0;j < col;j++) {
        [innerTmp addObject:@0];
    }
    [_tilesMatrix addObject:innerTmp];
}