gpt4 book ai didi

cocoa - NSOutlineView 子类崩溃 - NSRangeException

转载 作者:行者123 更新时间:2023-12-03 17:02:17 24 4
gpt4 key购买 nike

我有一个 NSOutlineView 的子类,它监听 NSManagedObjectContext 更改通知并相应地更新大纲 View 。我在某个时候遇到了奇怪的崩溃,我的用户报告了这一点(我自己无法重现)...崩溃本身是一个直接的 NSRangeException:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSIndexSet initWithIndexesInRange:]: Range {18446744073709551614, 1} exceeds maximum index value of NSNotFound - 1'

但令人困惑的部分是发生这种情况的代码:

- (void) addedObject: (CommonListData *) item toExistingSection: (CommonListData *) existingSection atIndex: (NSInteger) index {

if (index != NSNotFound) {
[self beginUpdates];
[self insertItemsAtIndexes: [NSIndexSet indexSetWithIndex: index] inParent: existingSection withAnimation:NSTableViewAnimationEffectFade | NSTableViewAnimationSlideDown];
[self endUpdates];

NSInteger scrollPosition = [self rowForItem: item];
if (scrollPosition != NSNotFound && scrollPosition !=0) {
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:scrollPosition - 1]; // crashes here

现在,如果我正在检查 NSNotFound,并且是否为 0,为什么 indexSetWithIndex:(scrollPosition-1) 仍然给出 NSRangeException?我还可以检查什么来确保 scollPosition 有效或无效?

不确定这是否相关,但只有当我的核心数据堆栈连接到 iCloud 并且我收到 NSPersistentStoreDidImportUbiquitousContentChangesNotification,并且我使用上下文对通知执行 mergeChangesFromContextDidSaveNotification 时,才会发生这种情况。

最佳答案

如果项目不在大纲中,

-[NSOutlineView rowForItem:] 返回 -1(而不是 NSNotFound)。因此,scrollPosition 为 -1,scrollPosition - 1 为 -2。 +[NSIndexSet indexSetWithIndex:] 采用一个(当然)无符号的 NSUInteger,因此 -2 变为 18446744073709551614。

关于cocoa - NSOutlineView 子类崩溃 - NSRangeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29572168/

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