gpt4 book ai didi

objective-c - 声明、分配、加载和释放 NSMutableArray 的正确方法

转载 作者:太空狗 更新时间:2023-10-30 03:39:53 31 4
gpt4 key购买 nike

我在我的 *.h 文件中声明我的数组:

@interface aViewController: UIViewController{   NSMutableArray *anArray;    // You will need to later change this many times.}@end

我为我的 *.m 文件分配内存:

-(void) viewDidLoad{   anArray = [[NSMutableArray alloc] init];}

我点击一个测试按钮来加载我的数组(最终它需要加载不同的值每次点击):

   anArray = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil];

我在这里释放它:

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

这一切看起来还好吗?

因为当我稍后运行这段代码时它崩溃了:

   NSLog(@"%d", [anArray count]);

不确定为什么简单的“NSLog() 和计数”会导致一切崩溃。


德克,

让我这样说吧:我对指针、数组、字符串和内存有巨大的误解。

我已经阅读了我能在上面找到的所有内容...但是(尚未)找到一个简单、清晰、易于理解的描述。

你能推荐一个吗? (希望少于 10 页阅读。)是否有解释只是这个主题的引用......并且从“你有 12 年的编码经验......但没有处理过分配内存或指针”的角度来看。)

所以变量名不是我引用对象的方式?那为什么要有呢?

我已经习惯了很多其他的语言:

myString = "this"myString = "that"myInt = 5myInt = 15

(还有什么可以更简单。)


在我看来,这是最简单的方法。(而且它似乎有效。但它真的是正确的吗?)

Don't alloc any memory for my NSMutableArray initially.Don't re-alloc any memory repeatedly.  (When I change my array's values.)Don't release it repeatedly. (Before I change my array's values.)Don't release it when I exit the program.But:Always remember to use RETAIN when I initially assign (and repeatedly reassign) new values to my anArray variable.

You are not loading your array with anArray = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil]; Instead, you are replacing it with a new instance, and worse: with an instance, whose reference is actually owned by some entity you do not control

哇。所以我可以有 20 个数组……都叫同一个名字:anArray……而且它们都不一样? (没有全局数组这样的东西吗?)

etc. In order to clear out old values, the method removeAllObject may be handy. There are also mutation methods, which may be used to add multiple values at once.

所以...首先我必须“删除所有对象”...然后我可以调用ONE 方法重新添加我所有的新值。

anArray = [[NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil] retain]; instead of the alloc/init sequence.

哇。我认为如果不为其分配空间,就无法将任何内容存储在数组中。

If you really intend to replace the entire array, you may want to consider using properties

我如何使用属性来做到这一点?
做这样的事情的正确方法是什么:

> anArray = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil];> anArray = [NSMutableArray arrayWithObjects:@"four", @"five", @"six", nil];

就像我会做的那样:

x = 12;x = 24;

哇。我真的完全误解了关于字符串、数组和内存的一切。我认为“简单的方法”是分配一次……使用可变数组……随心所欲地改变它……然后释放一次。

The problem with doing so is that this new array isn't retained,

我认为旧数组会消失......并且可以使用新数组。(但我想不是。)

Further, you have a memory leak because you never freed the original array.

我在想旧数组不应该被释放...我还没有完成它...我只是想更改它以包含我的新值。 (但我想不是。)

but one is to use [anArray release];

我在想这会导致我释放我分配的内存...(但我猜不会)...然后我必须重新分配更多内存。 (但我想不是。)

anArray = [[NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil] retain];

所以我必须“保留”它……所以它不会从我身下消失?(不确定为什么会这样。直到在我最后的 dealloc 调用中告诉它...)

Another, probably more correct way to fix it would be to use the addObject: or addObjectsFromArray: NSMutableArray methods instead of constantly creating new arrays.

我只想创建ONE 数组...然后按我的意愿使用它。我从不想ADD 到数组。我想将其设置为我的新值。

最佳答案

你没有加载你的数组

anArray = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil];

相反,您正在用一个新实例替换它,更糟糕的是:用一个实例,其引用实际上由您无法控制的某个实体拥有(很可能是一个 NSAutoreleasePool)。您正确拥有,由

创建的
[[NSMutableArray alloc] init]

丢失,会被泄露。

不是替换整个数组引用,而是改变你已经拥有的那个,使用例如 addObject: like

[anArray addObject: @"one"]
[anArray addObject: @"two"]

等为了清除旧值,removeAllObject 方法可能很方便。还有变异方法,可用于一次添加多个值。

或者,您可以使用您已经使用的构造方法分配数组,但要小心保留它。在 viewDidLoad 中执行

anArray = [[NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil] retain];

而不是 alloc/init 序列。

如果你真的打算替换整个数组,你可能要考虑使用 properties而不是手动进行引用计数。

关于objective-c - 声明、分配、加载和释放 NSMutableArray 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2308053/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com