首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么应用程序会与[NSObject(NSObject) doesNotRecognizeSelector:]崩溃?

为什么应用程序会与[NSObject(NSObject) doesNotRecognizeSelector:]崩溃?
EN

Stack Overflow用户
提问于 2017-01-21 14:32:20
回答 1查看 2.7K关注 0票数 1

这个应用程序有时会与[NSObject(NSObject) doesNotRecognizeSelector:]崩溃,选择器是[UIImageView setImage:]

我通过设置一个异常断点来捕获它,当在prepareForReuse of a UICollectionViewCell中设置一个UICollectionViewCell时,就会发生崩溃。

代码语言:javascript
复制
class MyCell: UICollectionViewCell {

    var coverImageView = UIImageView()

    ...

    override func prepareForReuse() {
        super.prepareForReuse()

        coverImageView.image = nil
    }
}

调用prepareForReuse,然后实例化单元格:

代码语言:javascript
复制
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

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

为什么会发生这种事?

另外,我无法加载UIImageView的快速查看数据视图,所以可能已经释放了对象?

控制台中的错误消息是:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2017-01-21 15:06:13

我认为问题在于您没有调用UIImageView的指定初始化器,因此它的image属性没有正确初始化,并且试图将应用程序分配给它:

-NSMallocBlock大小:未识别的选择器发送到实例0x608000880a0

该方法试图调用旧图像上的size方法,但旧图像被设置为一些随机内存地址。

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

代码语言:javascript
复制
var coverImageView = UIImageView(image: nil)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41780553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档