为什么我的 NSMutableArray 中的对象似乎无缘无故地失去了它们的值?

发布于 2024-12-21 07:07:47 字数 1449 浏览 4 评论 0原文

我在这里遇到了最奇怪的错误,我希望有人可以帮助我解决它。我正在将现有应用程序转换为 ARC(缓慢且痛苦)。我的应用程序是一个基本的图书阅读器应用程序,它使用 FMDB 与数据库对话。转换进行得很顺利,但是我现在崩溃了,我应该加载我的章节名称 tableView。

在我的头文件中,我创建了一个包含所有自定义卷对象的数组:

NSMutableArray * volumes;
}

@property (nonatomic, strong) NSMutableArray * volumes;

到目前为止我还好吗?在 .m 文件中,我合成变量,然后在 viewDidLoad 中,我用 db 调用的结果填充数组:

FMDatabase * database = [FMDatabase databaseWithName:name];

FMResultSet * set = [database executeQuery:@"SELECT * FROM book_volumes"];
volumes = [[NSMutableArray alloc] init];

while ([set next]) {
    Volume * v = [[Volume alloc] initWithResultSet:set];
    [volumes addObject:v];
}

这样做的结果是,我用 7 个对象填充了数组。如果我在调试器窗口中查看数组内部的值,它们都在那里,并且所有自定义对象都具有它们的所有属性。

当我点击播放时,它会转到 numberOfSectionsInTableView:,如果我第一次使用此方法查看数组的值,所有值仍然存在。但是当点击播放时,它会再次转到相同的方法,numberOfSectionsInTableView:。是这样吗?调用 numberOfSectionsInTableView: 两次?如果我在第二次调用期间查看数组中的值,仍然有 7 个对象,但我无法访问它们的任何值。他们仍然有地址,但尽管我尽力了,但还是不可用。

当我们进入 cellForRowAtIndexPath: 时,这尤其成问题。它崩溃是因为它无法访问卷数组中对象的数据。

我对 ARC 还很陌生,所以也许我遗漏了一些东西,但是 ARC 是否有可能允许过早释放数组的对象?

我不知道这里会发生什么。我成功地将数据从数据库检索到数组中,然后不知怎的,在我们填充 tableView 之前它就消失了。

我缺少什么?

谢谢!

更新:

所以它仍然不起作用,而且仍然让我发疯。我想我应该附上当我进入 numberOfRowsInSection: 方法时的屏幕截图,一切都被炸毁了:

再次更新:

好吧,最终的结果是 ARC 转换工具更改了我的自定义对象的属性,使其前面有 __unsafe_unretained 。我希望我能早点发现这一点。我把它拿出来并将属性更改为字符串,错误就消失了。

感谢大家看过这个。

I am experiencing the strangest error here, and I am hoping someone can help me through it. I am in the (slow and painful) process of converting my existing app to ARC. My app is a basic book reader app that uses FMDB to talk to the db. The conversion went fine, but I am crashing now where I am supposed to be loading up my chapter names tableView.

In my header file I make this array that holds all of my custom volume objects:

NSMutableArray * volumes;
}

@property (nonatomic, strong) NSMutableArray * volumes;

Am I good so far? In the .m file I synthesize my variable, and then in viewDidLoad I fill the array up with the result of a db call:

FMDatabase * database = [FMDatabase databaseWithName:name];

FMResultSet * set = [database executeQuery:@"SELECT * FROM book_volumes"];
volumes = [[NSMutableArray alloc] init];

while ([set next]) {
    Volume * v = [[Volume alloc] initWithResultSet:set];
    [volumes addObject:v];
}

The result of this is that I have the array filled up with 7 objects. If I look at the values inside of the array in the debugger window, they are all in there, and all of the custom objects have all of their properties.

When I hit play, it goes to numberOfSectionsInTableView:, and if I look at the values of my array while in this method the first time, all of the values are still there. But when hit play, it goes to the same method again, numberOfSectionsInTableView:. Is that right? To call numberOfSectionsInTableView: twice? If I look at the values in the array during this second call, there are still 7 objects, but I CAN'T ACCESS ANY OF THEIR VALUES. They have addresses still, but try as I might, they aren't available.

This is especially problematic when we go down to cellForRowAtIndexPath:. It crashes because it can't access the data on the objects in the volumes array.

I pretty new to ARC, so maybe I'm missing something, but is there a chance that ARC is allowing the array's objects to be released too early?

I have no clue what could be going on here. I am successfully retrieving the data from the db and into the array, and then somehow it just disappears before we get to filling the tableView.

What am I missing?

Thanks!

UPDATE:

So it still isn't working, and it is still driving me crazy. I thought I'd attach a screenshot of when I go into the numberOfRowsInSection: method, and everything gets blown up:

It doesn't make sense!

UPDATE AGAIN:

Ok so what it ended up being was that the ARC conversion tool changed the properties of my custom object to have __unsafe_unretained in front of it. I wish I would have caught that earlier. I took that out and changed the property to string and the error is gone.

Thanks for everybody looking this over.

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

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

发布评论

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

评论(2

桜花祭 2024-12-28 07:07:47

好吧,最终的结果是 ARC 转换工具更改了我的自定义对象的属性,使其前面有 __unsafe_unretained 。我希望我能早点发现这一点。我把它拿出来并将属性更改为强,错误就消失了。

感谢大家看过这个。

Ok so what it ended up being was that the ARC conversion tool changed the properties of my custom object to have __unsafe_unretained in front of it. I wish I would have caught that earlier. I took that out and changed the property to strong and the error is gone.

Thanks for everybody looking this over.

回梦 2024-12-28 07:07:47

尝试使用

self.volumes = [[NSMutableArray alloc] init];

而不是

volumes = [[NSMutableArray alloc] init];

Try using

self.volumes = [[NSMutableArray alloc] init];

instead of

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