NSMutableArray 正在毁掉我的生活!

发布于 2024-08-26 13:06:54 字数 3461 浏览 9 评论 0原文

IET 另一个编辑(以增加陌生感) 编辑以显示代码的相关部分

嗨。 NSMutableArray 有一个奇怪的问题,我只是不理解......

解释:

我有一个 NSMutableArray,定义为属性(非原子,保留),合成并用 29 个元素初始化。

realSectionNames = [[NSMutableArray alloc] initWithCapacity:29];

初始化后,我可以根据需要插入元素,一切似乎都工作正常。

但是,当我运行应用程序时,如果我在数组中插入一个新元素,我可以在插入该元素的函数中打印该数组,并且一切看起来都正常。

但是,当我在表中选择一行并且需要读取该数组时,我的应用程序崩溃了。事实上,它甚至无法再打印数组。

在使用 NSMutableArray 时,是否有任何“神奇和逻辑的技巧”是每个人都应该知道的,而像我这样的初学者可能会错过呢?

多谢。

时,我声明了我的数组

realSectionNames = [[NSMutableArray alloc] initWithCapacity:29];

当我在数组中插入对象

[realSectionNames addObject:[category categoryFirstLetter]];

,尽管我知道我也可以将其插入到

[realSectionNames insertObject:[category categoryFirstLetter] atIndex:i];

“i”是第一个未占用位置的位置。

插入后,我重新加载 tableView 的数据。在重新加载数据之前或之后打印数组表明它具有所需的信息。

之后,选择表中的一行会使应用程序崩溃。这个 realSectionNames 在多个 UITableViewDelegate 函数中使用,但对于这种情况来说并不重要。真正重要的是,在 didSelectRowAtIndexPath 函数的开头打印数组会导致所有内容崩溃(当然,不会打印任何内容)。我很确定它在那一行,用于在工作之前打印任何内容(示例):

NSLog(@"Anything");
NSLog(@"%@", realSectionNames);

给出输出:

2010-03-24 15:16:04.146 myApplicationExperience[3527:207] Anything

[会议开始于 2010-03-24 15:16:04 +0000。] GNU gdb 6.3.50-20050815(Apple 版本 gdb-967)(UTC 2009 年 7 月 14 日星期二 02:11:58) 版权所有 2004 自由软件基金会,Inc. GDB 是免费软件,受 GNU 通用公共许可证保护,您可以 欢迎在某些条件下对其进行更改和/或分发其副本。 输入“显示复制”以查看条件。 GDB 绝对没有任何保证。输入“显示保修”以了解详细信息。 该 GDB 被配置为“i386-apple-darwin”。sharedlibrary apply-load-rules all 附加到进程3527。

仍然不明白我这次做了什么样的愚蠢行为......也许现在追随脑外科医生的职业还为时不晚?

在回答之后,我打印了

NSLog(@"self=%x", self); 
NSLog(@"self=%@", self); 
NSLog(@"realSectionNames=%x", realSectionNames); 

每个函数(无论是否来自委托)都给出了完全相同的结果。

NSLog(@"realSections = %@", realSectionNames); 

在我的 viewWillAppear、didSelectRowAtIndexPath 中打印良好,在 viewForHeaderInSection 中崩溃。顺便说一句,没有线程...

所以,在不知道该怎么做的情况下,我正在尝试“事物”...我将 realSectionNames 的所有引用更改为 self.realSectionNames

在 vi​​ewForHeaderInSection 中打印给出了以下问题:

2010-03-24 16:01:44.067 myApplication[4104:207] numberOfSectionsInTableView result -> 1
2010-03-24 16:01:44.068 myApplication[4104:207] viewForHeaderAtSection
2010-03-24 16:01:44.068 myApplication[4104:207] self=3d13470
2010-03-24 16:01:44.068 myApplication[4104:207] self=<RootViewController: 0x3d13470>
2010-03-24 16:01:44.069 myApplication[4104:207] self.realSectionNames=3b12830
2010-03-24 16:01:44.070 myApplication[4104:207] realSections = (
    <__NSArrayReverseEnumerator: 0x3b50500>
)
2010-03-24 16:01:44.070 myApplication[4104:207] *** -[__NSArrayReverseEnumerator length]: unrecognized selector sent to instance 0x3b50500
2010-03-24 16:01:44.071 myApplication[4104:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayReverseEnumerator length]: unrecognized selector sent to instance 0x3b50500'
2010-03-24 16:01:44.072 myApplication[4104:207] Stack: (
    31073371,
    2572170505,
    31455291,
    31024758,
    30877378,
    276908,
    26404,
    3227182,
    4544033,
    4551926,
    4550923,
    3267462,
    3207973,
    3249408,
    25927,
    3222086,
    3205252,
    459178,
    30857920,
    30854216,
    39163413,
    39163610,
    2949039
)

什么是 NSArrayReverseEnumerator ???而且为什么对我这么刻薄???

IET ANOTHER EDIT (to increase strangeness)
EDITED to show the relevant part of the code

Hi. There's a strange problem with an NSMutableArray which I'm just not understanding...

Explaining:

I have a NSMutableArray, defined as a property (nonatomic, retain), synthesized, and initialized with 29 elements.

realSectionNames = [[NSMutableArray alloc] initWithCapacity:29];

After the initialization, I can insert elements as I wish and everything seems to be working fine.

While I'm running the application, however, if I insert a new element in the array, I can print the array in the function where I inserted the element, and everything seems ok.

However, when I select a row in the table, and I need to read that array, my application crashes. In fact, it cannot even print the array anymore.

Is there any "magical and logical trick" everybody should know when using a NSMutableArray that a beginner like myself can be missing?

Thanks a lot.

I declare my array as

realSectionNames = [[NSMutableArray alloc] initWithCapacity:29];

I insert objects in my array with

[realSectionNames addObject:[category categoryFirstLetter]];

although I know i can also insert it with

[realSectionNames insertObject:[category categoryFirstLetter] atIndex:i];

where the "i" is the first non-occupied position.

After the insertion, I reload the data of my tableView. Printing the array before or after reloading the data shows it has the desired information.

After that, selecting a row at the table makes the application crash. This realSectionNames is used in several UITableViewDelegate functions, but for the case it doesn't matter. What truly matters is that printing the array in the beginning of the didSelectRowAtIndexPath function crashes everything (and of course, doesn't print anything). I'm pretty sure it's in that line, for printing anything he line before works (example):

NSLog(@"Anything");
NSLog(@"%@", realSectionNames);

gives the output:

2010-03-24 15:16:04.146 myApplicationExperience[3527:207] Anything

[Session started at 2010-03-24 15:16:04 +0000.]
GNU gdb 6.3.50-20050815 (Apple version gdb-967) (Tue Jul 14 02:11:58 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 3527.

Still not understanding what kind of stupidity I've done this time... maybe it's not too late to follow the career of brain surgeon?

following an answer, i printed

NSLog(@"self=%x", self); 
NSLog(@"self=%@", self); 
NSLog(@"realSectionNames=%x", realSectionNames); 

gives the exact same results in every function (from delegate or not).

NSLog(@"realSections = %@", realSectionNames); 

prints well in my viewWillAppear, in the didSelectRowAtIndexPath, and crashes in viewForHeaderInSection. No threading, by the way...

So, without knowing what to do, I'm trying "things"... I changed all references of realSectionNames to self.realSectionNames

printing in the viewForHeaderInSection gives the following problem:

2010-03-24 16:01:44.067 myApplication[4104:207] numberOfSectionsInTableView result -> 1
2010-03-24 16:01:44.068 myApplication[4104:207] viewForHeaderAtSection
2010-03-24 16:01:44.068 myApplication[4104:207] self=3d13470
2010-03-24 16:01:44.068 myApplication[4104:207] self=<RootViewController: 0x3d13470>
2010-03-24 16:01:44.069 myApplication[4104:207] self.realSectionNames=3b12830
2010-03-24 16:01:44.070 myApplication[4104:207] realSections = (
    <__NSArrayReverseEnumerator: 0x3b50500>
)
2010-03-24 16:01:44.070 myApplication[4104:207] *** -[__NSArrayReverseEnumerator length]: unrecognized selector sent to instance 0x3b50500
2010-03-24 16:01:44.071 myApplication[4104:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayReverseEnumerator length]: unrecognized selector sent to instance 0x3b50500'
2010-03-24 16:01:44.072 myApplication[4104:207] Stack: (
    31073371,
    2572170505,
    31455291,
    31024758,
    30877378,
    276908,
    26404,
    3227182,
    4544033,
    4551926,
    4550923,
    3267462,
    3207973,
    3249408,
    25927,
    3222086,
    3205252,
    459178,
    30857920,
    30854216,
    39163413,
    39163610,
    2949039
)

What is a NSArrayReverseEnumerator??? And why is it being mean to me???

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

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

发布评论

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

评论(4

旧城空念 2024-09-02 13:06:54

请大家注意(特别是自己)……

我是一个非常非常愚蠢的男孩。

我正在释放插入数组中的一个元素。

抱歉提出问题,谢谢各位的解答...

Note to everybody (and specially to self)...

I'm a dumb, dumb boy.

I was releasing an element which I inserted in the array.

Sorry for the questions, thanks for the answers...

薆情海 2024-09-02 13:06:54

当应用程序崩溃时,Xcode控制台将报告导致应用程序崩溃的错误。您需要编辑您的问题以包含此错误消息以及相关源代码,因为这将帮助其他人回答您的问题。

我怀疑您选择的表行试图指向数组中不存在的索引。如果是这样,您正在尝试引用数组中不存在的部分。这会导致您的应用程序抛出异常并退出。

请记住,NSMutableArray 可以初始化为一定的容量,但在插入项目之前,它实际上并不容纳任何对象。 -initWithCapacity: 方法仅预留一定量的内存,但 1n 没有占位符或 nil 条目 索引。

When an application crashes, the Xcode Console will report the error that caused the application to crash. You will want to edit your question to include this error message, along with relevant source code, as this will help others answer your question.

I suspect your selected table row is trying to point to an index in the array, which does not exist. If so, you're trying to reference a part of the array that does not exist. This causes your application to throw an exception and quit.

Remember that an NSMutableArray can be initialized to a certain capacity, but until items are inserted, it does not actually hold any objects. The -initWithCapacity: method only sets aside a certain amount of memory, but there are no placeholders or nil entries for 1 through n indices.

飘落散花 2024-09-02 13:06:54

1)我们能知道错误信息吗?以及代码的相关部分?

2)这是声明数组的正确方法:

self.realSectionNames = [[NSMutableArray arrayWithCapacity:29];

1) Could we know the error message ? and the relevant part of the code ?

2) Here is the proper way to declare your array :

self.realSectionNames = [[NSMutableArray arrayWithCapacity:29];
眼泪淡了忧伤 2024-09-02 13:06:54

我怀疑您的 TableView 委托未正确连接,或者连接到无效对象。您的其他委托方法是否有效?尝试将它们放在 didSelectRowAtIndexPath 的顶部以检查指针值:

NSLog(@"self=%x", self);
NSLog(@"self=%@", self);
NSLog(@"realSectionNames=%x", realSectionNames);

I suspect your TableView's delegate isn't hooked up correctly, or is connected to an invalid object. Are any of your other delegate methods working? Try putting these at the top of didSelectRowAtIndexPath to check the pointer values:

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