向数组添加新元素时出现异常错误导致应用程序崩溃

5
我正在从一个API中获取物品列表,这个功能运行良好。在我的视图控制器中有一个本地变量,以便在成功从API获取项目后,我正在重新加载我的表格视图。
最近,在我将来自API的数组内容附加到我的本地数组变量的行中,我现在遇到了崩溃问题。我已经进行了多次研究,但没有成功。我一直收到相同的错误。
以下是导致崩溃的代码:

与 API 抓取无关的代码


     if refresh {
                    self?.items = ticketItems
                }
                else {
                    self?.ticketItems.append(contentsOf: 
                    ticketItems)
                }

通常情况下,当我追加数组的内容时不应该出现崩溃。这对我来说很奇怪。请查找错误信息:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: !CGSizeEqualToSize(size, CGSizeZero)'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000116bb86fb __exceptionPreprocess + 331
    1   libobjc.A.dylib                     0x00000001157adac5 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000116bb8482 +[NSException raise:format:arguments:] + 98
    3   Foundation                          0x000000010fe51927 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 194
    4   UIKitCore                           0x000000011e0f0399 -[_UIUpdateVisibleCellsContext addSize:forIndexPath:] + 385
    5   UIKitCore                           0x000000011e0e8643 -[UICollectionViewFlowLayout shouldInvalidateLayoutForPreferredLayoutAttributes:withOriginalAttributes:] + 409
    6   UIKitCore                           0x000000011e0a2f08 -[UICollectionView _checkForPreferredAttributesInView:originalAttributes:] + 559
    7   UIKitCore                           0x000000011e0a3c24 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 1596
    8   UIKitCore                           0x000000011e0a35e2 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 31
    9   UIKitCore                           0x000000011e0a8a8c -[UICollectionView _updateVisibleCellsNow:] + 6120
    10  UIKitCore                           0x000000011e0adbb6 -[UICollectionView layoutSubviews] + 365
    11  UIKitCore                           0x000000011ecf99c1 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1417
    12  QuartzCore                          0x0000000111cf7eae -[CALayer layoutSublayers] + 173
    13  QuartzCore                          0x0000000111cfcb88 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 396
    14  QuartzCore                          0x0000000111d08ee4 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 72
    15  QuartzCore                          0x0000000111c783aa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 328
    16  QuartzCore                          0x0000000111caf584 _ZN2CA11Transaction6commitEv + 608
    17  QuartzCore                          0x0000000111cafede _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 76
    18  CoreFoundation                      0x0000000116b1f0f7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    19  CoreFoundation                      0x0000000116b195be __CFRunLoopDoObservers + 430
    20  CoreFoundation                      0x0000000116b19c31 __CFRunLoopRun + 1505
    21  CoreFoundation                      0x0000000116b19302 CFRunLoopRunSpecific + 626
    22  GraphicsServices                    0x000000011be162fe GSEventRunModal + 65
    23  UIKitCore                           0x000000011e82bba2 UIApplicationMain + 140
    24  Sterling Bank App                   0x000000010c1be211 main + 225
    25  libdyld.dylib                       0x0000000118307541 start + 1
    26  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

你是否启用了异常断点?并找到了导致崩溃的确切代码? - Vicky_Vignesh
是的,我有。它仍然指向我在上面问题中编写的同一行代码,除非我没有做正确。@Vicky_Vignesh,您可以帮忙再详细解释一下吗? - codeperfect
错误与您的数组完全无关。异常明确指出某个 CGSize 为零。我猜测这是您的集合视图布局问题,从堆栈跟踪中可以看出来。 - Alladinian
@codeperfect,当您的断点处于活动状态并指向确切的代码时,您能否附上一张截图?这肯定会对我们有所帮助! - Vicky_Vignesh
你最终解决了这个错误吗?我遇到了完全相同的错误,几乎相同的回溯,并且我认为问题是由于UICollectionView中的某个项目大小为(0, 0)引起的。 - JacobCXDev
1个回答

4

我遇到了如下错误:

'参数无效,不满足条件:!CGSizeEqualToSize(size, CGSizeZero)'

这是因为我忘记将要添加到单元格 contentView 的子视图的 translatesAutoresizingMaskIntoConstraints 设置为 false


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接