1

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

在此处输入图像描述

我通过设置异常断点来捕获它,并且在设置 a 时UIImageView.image发生prepareForReuse崩溃UICollectionViewCell

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
4

1 回答 1

1

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

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

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

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

var coverImageView = UIImageView(image: nil)
于 2017-01-21T15:06:13.170 回答