复制多维 NSMutableArray

发布于 2024-09-12 05:15:56 字数 952 浏览 7 评论 0原文

我目前正在开发数独应用程序,数字存储在 NSNumbers 的多维 NSMutableArray 中。我在 SudokuGridView 中保留一个数组,用于在网格中显示数字。当需要解决这个难题时,我将 [grid numberGrid] 传递给我创建的 NSOperation 子类来解决这个难题。

网格的数组被定义为一个属性,如下所示:

@property (readonly) NSMutableArray *numberArray; 

当将其传递给数独网格求解器时,我会执行:

MESudokuSolver *solvePuzzleOperation  = [[MESudokuSolver alloc] initWithPuzzle: [grid numberArray]];

initWithPuzzle 定义如下:

- (id)initWithPuzzle:(NSMutableArray *)puzzleArray  {
    if(self = [super init]) {
        puzzle = [[NSMutableArray alloc] initWithArray: puzzleArray];
    }
    return self;    
}

当我将谜题转换为原始 int 数组来求解时,然后返回谜题 NSMutableArray 。有趣的是,现在,网格的 NSMutableArray 现在有了解决方案......这意味着在 MESudokuSolver 内部,网格的数组正在被修改。所以我做了一些调查,传递到 MESudokuSolver 实例的数组指针与 MESudokuSolver 的拼图 NSMutableArray 不同。奇怪吧?我知道。

经过进一步调查,具有不同指针的数组内指向 NSNumbers 的指针实际上是相同的。

我问你 StackOverflow,WTF?

I'm currently working on a Sudoku application, the numbers are stored within a Multi-Dimensional NSMutableArray of NSNumbers. I keep an array in my SudokuGridView, for displaying the numbers in the grid. When it comes time to solve the puzzle, I pass a [grid numberGrid] to a subclass of NSOperation I've created that solves the puzzle.

The grid's array is defined as a property as such:

@property (readonly) NSMutableArray *numberArray; 

When passing it to the sudoku grid solver I go:

MESudokuSolver *solvePuzzleOperation  = [[MESudokuSolver alloc] initWithPuzzle: [grid numberArray]];

initWithPuzzle is defined as so:

- (id)initWithPuzzle:(NSMutableArray *)puzzleArray  {
    if(self = [super init]) {
        puzzle = [[NSMutableArray alloc] initWithArray: puzzleArray];
    }
    return self;    
}

When I then convert the puzzle to a primitive int array to solve it, and then back into the puzzle NSMutableArray. What's funny, is that now, the grid's NSMutableArray now has the solution... Which means that somehow inside the MESudokuSolver the grid's array is being modified. So I did some investigation, the pointer to the array that is passed into the MESudokuSolver instance is different than the MESudokuSolver's puzzle NSMutableArray. Strange, right? I know.

Upon FURTHER investigation, the pointer to the NSNumbers inside the arrays with different pointers are actually the SAME.

To you StackOverflow, I ask, WTF?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见终念 2024-09-19 05:15:56

当您使用另一个数组的内容初始化一个数组时,两个数组的内容将引用相同的对象。您想要做的是执行深层复制。这可确保每个数组引用其自己的对象副本,因此,如果您修改一个数组中的对象,则不会影响另一个数组中的对象,因为它们实际上是不同的对象。这甚至适用于数组的数组。有多种方法可以执行深度复制。由于您希望可变数组中存在可变数组的可变副本,因此这有点棘手,但仍然很简单:

// Implemented as a free function here, but this is not required.

NSMutableArray *MECopyGrid(NSMutableArray *outer)
{
    NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:[outer count]];

    for (NSMutableArray *inner in outer)
    {
        NSMutableArray *theCopy = [inner mutableCopy];
        [result addObject:theCopy];
        [theCopy release];
    }

    return result;
}

还要注意 NSNumber 优化。 Cocoa(我假设 Cocoa Touch 也一样)缓存了一些不同的 NSNumber 实例。由于 NSNumber 实例是不可变的,如果您请求 [NSNumber numberWithInteger:1],Cocoa 可能会为您提供对包含相同值的现有实例的引用。如果您注意到 NSNumber 实例指针是相同的,很可能是因为 Cocoa 给了您一个旧实例。这将节省内存,尤其是在像您这样的情况下(如果没有优化,您将需要 81 个独立的 NSNumber 实例,但通过优化,您最多只需要 9 个)。

When you initialise an array with the contents of the another array, the contents of both arrays will reference the same objects. What you want to do is perform a deep copy. This ensures that each array references its own copy of the object, so that if you modify the object in one array, it does not affect the object in the other array because they are actually different objects. This applies even to arrays of arrays. There are a number of approaches to performing deep copies. Since you want mutable copies of your mutable arrays inside your mutable array, it is a little trickier, but easy nonetheless:

// Implemented as a free function here, but this is not required.

NSMutableArray *MECopyGrid(NSMutableArray *outer)
{
    NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:[outer count]];

    for (NSMutableArray *inner in outer)
    {
        NSMutableArray *theCopy = [inner mutableCopy];
        [result addObject:theCopy];
        [theCopy release];
    }

    return result;
}

Beware also of NSNumber optimisations. Cocoa (and I assume Cocoa Touch also) caches a few different NSNumber instances. Since NSNumber instances are immutable, if you ask for [NSNumber numberWithInteger:1], Cocoa may give you a reference to an existing instance containing the same value. If you notice that NSNumber instance pointers are the same, it is likely because Cocoa has given you an old instance. This would save memory especially in situations like yours (without optimisations you would need 81 independent instances of NSNumber, but with optimisations you'll need at most only 9).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文