【问题标题】:Why app crashes with [NSObject(NSObject) doesNotRecognizeSelector:]?为什么应用程序会因 [NSObject(NSObject) doesNotRecognizeSelector:] 而崩溃?
【发布时间】:2017-06-06 10:33:48
【问题描述】:

应用程序有时会因[NSObject(NSObject) doesNotRecognizeSelector:] 而崩溃,选择器为[UIImageView setImage:]

我通过设置异常断点来捕获它,当在prepareForReuseUICollectionViewCell 中设置UIImageView.image 时发生崩溃:

class MyCell: UICollectionViewCell {

    var coverImageView = UIImageView()

    ...

    override func prepareForReuse() {
        super.prepareForReuse()

        coverImageView.image = nil
    }
}

prepareForReuse 被调用然后单元被实例化:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = myCollectionView.dequeueReusableCell(withReuseIdentifier: "myReuseID", for: indexPath) as! MyCell
    ...
}

为什么会这样?

我也无法加载UIImageView 的快速查看数据视图,所以该对象可能已被释放?

控制台中的错误信息是:

2017-01-21 15:56:19.653 MyApp[4873:20387361] -[__NSMallocBlock__ size]: unrecognized selector sent to instance 0x608000880aa0
2017-01-21 15:56:37.697 MyApp[4873:20387361] invalid mode 'kCFRunLoopCommonModes' provided to CFRunLoopRunSpecific - break on _CFRunLoopError_RunCalledWithInvalidMode to debug. This message will only appear once per execution.

2017-01-21 15:56:38.847 MyApp[4873:20617657] [Optimizely Logging]: Successfully saved data file to disk. Code revision is 229
2017-01-21 15:56:42.723 MyApp[4873:20387361] [Optimizely Logging]: (ERROR) NSInvalidArgumentException: Stack Trace:
(
    0   CoreFoundation                      0x000000010f587d4b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010ef6121e objc_exception_throw + 48
    2   CoreFoundation                      0x000000010f5f7f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x000000010f50d005 ___forwarding___ + 1013
    4   CoreFoundation                      0x000000010f50cb88 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010ba5e20f -[UIImageView _updateImageViewForOldImage:newImage:] + 297
    6   UIKit                               0x000000010ba59950 -[UIImageView setImage:] + 391
    7   MyApp                              0x000000010945e0d8 _TFC6MyApp23MyCell15prepareForReusefT_T_ + 136
    8   MyApp                              0x000000010945e112 _TToFC6MyApp23MyCell15prepareForReusefT_T_ + 34
    9   UIKit                               0x000000010c19aed1 -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 773
    10  UIKit                               0x000000010c19b8ea -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:] + 169
    11  MyApp                              0x00000001095bdee2 _TFC6MyApp23MyViewController14collectionViewfTCSo16UICollectionView13cellForItemAtV10Foundation9IndexPath_CSo20UICollectionViewCell + 594
    12  MyApp                              0x00000001095be747 _TToFC6MyApp23MyViewController14collectionViewfTCSo16UICollectionView13cellForItemAtV10Foundation9IndexPath_CSo20UICollectionViewCell + 87
    13  UIKit                               0x000000010c18675f -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 467
    14  UIKit                               0x000000010c186586 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 35
    15  UIKit                               0x000000010c18ba5e -[UICollectionView _updateVisibleCellsNow:] + 4803
    16  UIKit                               0x000000010c191725 -[UICollectionView layoutSubviews] + 313
    17  UIKit                               0x000000010b90dab8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
    18  QuartzCore                          0x000000010d558bf8 -[CALayer layoutSublayers] + 146
    19  QuartzCore                          0x000000010d54c440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
    20  QuartzCore                          0x000000010d54c2be _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    21  QuartzCore                          0x000000010d4da318 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
    22  QuartzCore                          0x000000010d5073ff _ZN2CA11Transaction6commitEv + 475
    23  QuartzCore                          0x000000010d507d6f _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113
    24  CoreFoundation                      0x000000010f52c267 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    25  CoreFoundation                      0x000000010f52c1d7 __CFRunLoopDoObservers + 391
    26  CoreFoundation                      0x000000010f510f8e __CFRunLoopRun + 1198
    27  CoreFoundation                      0x000000010f510884 CFRunLoopRunSpecific + 420
    28  GraphicsServices                    0x0000000112b77a6f GSEventRunModal + 161
    29  UIKit                               0x000000010b848c68 UIApplicationMain + 159
    30  MyApp                              0x00000001092f5a7f main + 111
    31  libdyld.dylib                       0x00000001115b568d start + 1
)
2017-01-21 15:56:42.725 MyApp[4873:20387361] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSMallocBlock__ size]: unrecognized selector sent to instance 0x608000880aa0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010f587d4b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010ef6121e objc_exception_throw + 48
    2   CoreFoundation                      0x000000010f5f7f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x000000010f50d005 ___forwarding___ + 1013
    4   CoreFoundation                      0x000000010f50cb88 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010ba5e20f -[UIImageView _updateImageViewForOldImage:newImage:] + 297
    6   UIKit                               0x000000010ba59950 -[UIImageView setImage:] + 391
    7   MyApp                              0x000000010945e0d8 _TFC6MyApp23MyCell15prepareForReusefT_T_ + 136
    8   MyApp                              0x000000010945e112 _TToFC6MyApp23MyCell15prepareForReusefT_T_ + 34
    9   UIKit                               0x000000010c19aed1 -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 773
    10  UIKit                               0x000000010c19b8ea -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:] + 169
    11  MyApp                              0x00000001095bdee2 _TFC6MyApp23MyViewController14collectionViewfTCSo16UICollectionView13cellForItemAtV10Foundation9IndexPath_CSo20UICollectionViewCell + 594
    12  MyApp                              0x00000001095be747 _TToFC6MyApp23MyViewController14collectionViewfTCSo16UICollectionView13cellForItemAtV10Foundation9IndexPath_CSo20UICollectionViewCell + 87
    13  UIKit                               0x000000010c18675f -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 467
    14  UIKit                               0x000000010c186586 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 35
    15  UIKit                               0x000000010c18ba5e -[UICollectionView _updateVisibleCellsNow:] + 4803
    16  UIKit                               0x000000010c191725 -[UICollectionView layoutSubviews] + 313
    17  UIKit                               0x000000010b90dab8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237
    18  QuartzCore                          0x000000010d558bf8 -[CALayer layoutSublayers] + 146
    19  QuartzCore                          0x000000010d54c440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
    20  QuartzCore                          0x000000010d54c2be _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    21  QuartzCore                          0x000000010d4da318 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280
    22  QuartzCore                          0x000000010d5073ff _ZN2CA11Transaction6commitEv + 475
    23  QuartzCore                          0x000000010d507d6f _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113
    24  CoreFoundation                      0x000000010f52c267 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    25  CoreFoundation                      0x000000010f52c1d7 __CFRunLoopDoObservers + 391
    26  CoreFoundation                      0x000000010f510f8e __CFRunLoopRun + 1198
    27  CoreFoundation                      0x000000010f510884 CFRunLoopRunSpecific + 420
    28  GraphicsServices                    0x0000000112b77a6f GSEventRunModal + 161
    29  UIKit                               0x000000010b848c68 UIApplicationMain + 159
    30  MyApp                              0x00000001092f5a7f main + 111
    31  libdyld.dylib                       0x00000001115b568d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

【问题讨论】:

  • 也许从 Xib 为该变量分配了不同的类?
  • 在您的崩溃日志中,缺少 0 到 4 之间的步骤。您也可以扩展一下吗?
  • @Sulthan 我无法展开它,它不是崩溃日志,而是 Xcoce 中的调试导航器。另外我认为没有任何步骤 1-3,因为 setImage 是它似乎崩溃的选择器。
  • @Sulthan 我没有使用 Xib,UICollectionViewUICollecitonViewCell 是在代码中创建的。
  • 取消选择调试导航器底部的过滤器。

标签: swift uiimageview uicollectionview uicollectionviewcell prepareforreuse


【解决方案1】:

我认为问题在于您没有调用UIImageView 的指定初始化程序,因此其image 属性未正确初始化并尝试分配给它会使应用程序崩溃:

-[NSMallocBlock size]:无法识别的选择器发送到实例 0x608000880aa0

该方法尝试在旧图像上调用size 方法,但旧图像被设置为某个随机内存地址。

要修复它,请使用指定的初始化程序正确初始化UIImageView

var coverImageView = UIImageView(image: nil)

【讨论】:

  • 有道理,我会试试的。但是,为什么还有公共初始化程序 UIImageView(void) 呢?如果它导致这样的崩溃,它不应该是私有的吗?
  • @Manuel 它继承自NSObject(和UIView)。在 Objective-C 中,初始化器的继承并不像在 Swift 中那么简单。
  • 我明白了,您如何解释它只是有时会崩溃?
  • UIImageView() 的初始化程序更改为 UIImageView(image: nil)UIView()UIView(frame: CGRect.zero) 但崩溃仍在发生。
猜你喜欢
  • 1970-01-01
  • 2013-10-19
  • 1970-01-01
  • 1970-01-01
  • 2011-03-04
  • 2023-03-26
  • 1970-01-01
  • 2015-02-12
  • 1970-01-01
相关资源
最近更新 更多