如何动态创建一个带有循环的2d NSMutableArray [英] How to dynamically create a 2d NSMutableArray with loops

查看:231
本文介绍了如何动态创建一个带有循环的2d NSMutableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处创建了原始帖子

首先,我尝试填充一个CGPoint ** allPaths。

At first, I tried to fill up a CGPoint **allPaths.

第一个维度是a路径,第二维度是路径点。

With the first dimension being "a path" and the second dimension being "the waypoint". And I get a CGPoint out of this.

例如:allPaths [0] [2]会给我第一个路径的CGPoint,第三个路径。

For example : allPaths[0][2] would give me the CGPoint of the first path, third waypoint.

我成功地在平原C中做了令人讨厌的循环。现在我想用NSMutableArrays在Obj-C中做同样的事情。

I successfully did it in plain C with nasty loops. And now I am trying to do the same thing in Obj-C with NSMutableArrays.

这是我的代码:

CCTMXObjectGroup *path;
NSMutableDictionary *waypoint;

int pathCounter = 0;
int waypointCounter = 0;

NSMutableArray *allPaths = [[NSMutableArray alloc] init];
NSMutableArray *allWaypointsForAPath = [[NSMutableArray alloc] init];

//Get all the Paths
while ((path = [self.tileMap objectGroupNamed:[NSString stringWithFormat:@"Path%d", pathCounter]]))
{
    waypointCounter = 0;
    //Empty all the data of the waypoints (so I can reuse it)
    [allWaypointsForAPath removeAllObjects];

    //Get all the waypoints from the path
    while ((waypoint = [path objectNamed:[NSString stringWithFormat:@"Wpt%d", waypointCounter]]))
    {
        int x = [[waypoint valueForKey:@"x"] intValue];
        int y = [[waypoint valueForKey:@"y"] intValue];

        [allWaypointsForAPath addObject:[NSValue valueWithCGPoint:CGPointMake(x, y)]];
        //Get to the next waypoint
        waypointCounter++;
    }

    //Add the waypoints of the path to the list of paths
    [allPaths addObject:allWaypointsForAPath];

    //Get to the next path
    pathCounter++;
}


$ b $ p

我的实际问题是所有路径中的所有路径都等于最后一个路径。 (所有第一个路径都被最后一个路径覆盖)

My actual problem is that all paths in allPaths are equal to the last one. (all the first paths are overrided with the last one)

我知道这是因为这行[allPaths addObject:allWaypointsForAPath]。

I know that it is because of this line [allPaths addObject:allWaypointsForAPath].

但是,我该怎么办呢?

推荐答案


不确定内存问题,但我想垃圾收集器应该工作?

Oh I think I found out something ! Not sure about memory issue, but I guess the garbage collector should work ?

实际上,我只需要声明NSMutableArray * allWaypointsForAPath在循环如下:

Actually, I just have to declaremy NSMutableArray *allWaypointsForAPath in the loop like this :

int pathCounter = 0;
int waypointCounter = 0;

NSMutableArray *allPaths = [[NSMutableArray alloc] init];

//Get all the PathZ
while ((path = [self.tileMap objectGroupNamed:[NSString stringWithFormat:@"Path%d", pathCounter]]))
{
    waypointCounter = 0;
    NSMutableArray *allWaypointsForAPath = [[NSMutableArray alloc] init];
    //Get all the waypoints from the path
    while ((waypoint = [path objectNamed:[NSString stringWithFormat:@"Wpt%d", waypointCounter]]))
    {
        int x = [[waypoint valueForKey:@"x"] intValue];
        int y = [[waypoint valueForKey:@"y"] intValue];
        [allWaypointsForAPath addObject:[NSValue valueWithCGPoint:CGPointMake(x, y)]];
        //Get to the next waypoint
        waypointCounter++;
    }

    [allPaths addObject:allWaypointsForAPath];
    //Get to the next path
    pathCounter++;
}

这篇关于如何动态创建一个带有循环的2d NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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