微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

IBDesignables代理崩溃

如何解决IBDesignables代理崩溃

我正在使用以下代码向ios应用中的一些视图添加可设计的功能

@IBDesignable
class DesignableView: UIView {
}

@IBDesignable
class DesignableButton: UIButton {
}

@IBDesignable
class DesignableLabel: UILabel {
}


extension UIView {
  
  @IBInspectable
  var cornerRadius: CGFloat {
    get {
      return layer.cornerRadius
    }
    set {
      layer.cornerRadius = newValue
    }
  }
  
  @IBInspectable
  var borderWidth: CGFloat {
    get {
      return layer.borderWidth
    }
    set {
      layer.borderWidth = newValue
    }
  }
  
  @IBInspectable
  var borderColor: UIColor? {
    get {
      if let color = layer.borderColor {
        return UIColor(cgColor: color)
      }
      return nil
    }
    set {
      if let color = newValue {
        layer.borderColor = color.cgColor
      } else {
        layer.borderColor = nil
      }
    }
  }
  
  @IBInspectable
  var shadowRadius: CGFloat {
    get {
      return layer.shadowRadius
    }
    set {
      layer.shadowRadius = newValue
    }
  }
  
  @IBInspectable
  var shadowOpacity: Float {
    get {
      return layer.shadowOpacity
    }
    set {
      layer.shadowOpacity = newValue
    }
  }
  
  @IBInspectable
  var shadowOffset: CGSize {
    get {
      return layer.shadowOffset
    }
    set {
      layer.shadowOffset = newValue
    }
  }
  
  @IBInspectable
  var shadowColor: UIColor? {
    get {
      if let color = layer.shadowColor {
        return UIColor(cgColor: color)
      }
      return nil
    }
    set {
      if let color = newValue {
        layer.shadowColor = color.cgColor
      } else {
        layer.shadowColor = nil
      }
    }
  }
}

在未加载情节提要的情况下,如何不断出现“代理程序崩溃”的信息。该应用程序可以编译并正常运行。

崩溃日志如下:

Crashed Thread:        0

Exception Type:        EXC_CRASH (SIGABRT)
Exception Codes:       0x0000000000000000,0x0000000000000000
Exception Note:        EXC_CORPSE_NOTIFY

Application Specific information:
*** Terminating app due to uncaught exception 'NSinvalidargumentexception',reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'
terminating with uncaught exception of type NSException
abort() called
CoreSimulator 704.12.2 - Device: iPad Pro (9.7-inch) (6CED57CA-A03B-463F-9399-C033AD65EB86) - Runtime: iOS 13.7 (17H22) - DeviceType: iPad Pro (9.7-inch)

Application Specific Backtrace 1:
0   CoreFoundation                      0x00007fff23e3de6e __exceptionPreprocess + 350
1   libobjc.A.dylib                     0x00007fff512a19b2 objc_exception_throw + 48
2   CoreFoundation                      0x00007fff23ed09d1 _CFThrowFormattedException + 194
3   CoreFoundation                      0x00007fff23edb426 -[__NSPlaceholderDictionary initWithObjects:forKeys:count:].cold.4 + 38
4   CoreFoundation                      0x00007fff23e9c6e7 -[__NSPlaceholderDictionary initWithObjects:forKeys:count:] + 247
5   CoreFoundation                      0x00007fff23e39d71 +[NSDictionary dictionaryWithObjects:forKeys:count:] + 49
6   UIKitCore                           0x00007fff497b8d85 +[UILabel _defaultAttributes] + 403
7   UIKitCore                           0x00007fff48d0cb29 __35+[UIButtonLabel _defaultAttributes]_block_invoke + 56
8   libdispatch.dylib                   0x00007fff520dc8cb _dispatch_client_callout + 8
9   libdispatch.dylib                   0x00007fff520ddb02 _dispatch_once_callout + 20
10  UIKitCore                           0x00007fff48d0caef +[UIButtonLabel _defaultAttributes] + 97
11  UIKitCore                           0x00007fff48d162a3 -[UIButton _deriveTitleRect:imageRect:fromContentRect:calculatePositionForEmptyTitle:] + 2312
12  UIKitCore                           0x00007fff48d15878 -[UIButton _titleRectForContentRect:calculatePositionForEmptyTitle:] + 287
13  UIKitCore                           0x00007fff48d1591f -[UIButton titleRectForContentRect:] + 49
14  UIKitCore                           0x00007fff48d1af14 -[UIButton _setupTitleViewRequestingLayout:] + 156
15  UIKitCore                           0x00007fff48d10e5f -[UIButton titleLabel] + 42
16  Foundation                          0x00007fff2590f8ad -[NSObject(NSkeyvalueCoding) valueForKey:] + 317
17  Foundation                          0x00007fff259105ae -[NSObject(NSkeyvalueCoding) setValue:forKeyPath:] + 251
18  IBCocoaTouchToolFoundation          0x00000001066452e3 __IBApplyMarshalledState_block_invoke + 425
19  IBCocoaTouchToolFoundation          0x0000000106644bc5 IBApplyMarshalledState + 512
20  IBCocoaTouchToolFoundation          0x0000000106624c62 -[UIButton(IBCocoaTouchToolIntegration) initWithMarshalledValues:orderedKeys:ignoredKeys:globalMarshallingContext:] + 302
21  IBCocoaTouchToolFoundation          0x0000000106645441 +[NSObject(IBCocoaTouchToolIntegration) instantiateWithMarshalledValues:orderedKeys:ignoredKeys:globalMarshallingContext:] + 118
22  IBCocoaTouchToolFoundation          0x00000001066619a8 -[IBCocoaTouchToolObjectPackage initWithRequest:globalMarshallingContext:] + 2473
23  IBCocoaTouchToolFoundation          0x00000001066608c0 +[IBCocoaTouchToolObjectPackage objectPackageFromrequest:] + 51
24  IBCocoaTouchToolFoundation          0x000000010666396c +[IBCocoaTouchToolObjectPackage decodeWithBinaryUnarchiver:] + 106
25  IBFoundation                        0x000000010682d771 -[IBBinaryUnarchiver decodeObject] + 109
26  IBFoundation                        0x00000001068d2e09 -[IBObjectBasedMarshallingRequest initWithBinaryUnarchiver:] + 128
27  IBAutolayoutFoundation              0x000000010678b366 -[IBFullSceneUpdateRequest initWithBinaryUnarchiver:] + 65
28  IBFoundation                        0x0000000106824499 +[NSObject(IBBinaryArchivingAdditions) decodeWithBinaryUnarchiver:] + 49
29  IBFoundation                        0x000000010682d771 -[IBBinaryUnarchiver decodeObject] + 109
30  IBFoundation                        0x000000010682db63 -[IBBinaryUnarchiver decodeObjectReferenceIfPossible] + 74
31  IBFoundation                        0x00000001068249b5 -[NSArray(IBBinaryArchivingAdditions) initWithBinaryUnarchiver:] + 184
32  IBFoundation                        0x0000000106824499 +[NSObject(IBBinaryArchivingAdditions) decodeWithBinaryUnarchiver:] + 49
33  IBFoundation                        0x000000010682d771 -[IBBinaryUnarchiver decodeObject] + 109
34  IBFoundation                        0x000000010691e8f9 -[IBMessageReceiveChannel deliverMessage:toTarget:withArguments:context:result:] + 400
35  IBFoundation                        0x000000010691e336 __88-[IBMessageReceiveChannel runBlockingReceiveLoopNotifyingQueue:notifyingTarget:context:]_block_invoke + 142
36  libdispatch.dylib                   0x00007fff520dc8cb _dispatch_client_callout + 8
37  libdispatch.dylib                   0x00007fff520e9940 _dispatch_async_and_wait_invoke + 109
38  libdispatch.dylib                   0x00007fff520dc8cb _dispatch_client_callout + 8
39  libdispatch.dylib                   0x00007fff520e8cdb _dispatch_main_queue_callback_4CF + 1042
40  CoreFoundation                      0x00007fff23da1869 __CFRUNLOOP_IS_SERVICING_THE_MAIN_disPATCH_QUEUE__ + 9
41  CoreFoundation                      0x00007fff23d9c3b9 __CFRunLoopRun + 2041
42  CoreFoundation                      0x00007fff23d9b8a4 CFRunLoopRunSpecific + 404
43  Foundation                          0x00007fff25967c71 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 211
44  IBFoundation                        0x00000001068d99cd -[IBAbstractPlatformTool startServingReceiveChannel:] + 418
45  IBFoundation                        0x00000001068d9c29 -[IBAbstractPlatformTool startServingWriteDescriptor:readDescriptor:] + 111
46  IBFoundation                        0x00000001068daa8e +[IBAbstractPlatformTool main] + 1209
47  IBDesignablesAgent-iOS              0x000000010656e95f main + 34
48  libdyld.dylib                       0x00007fff5211c1fd start + 1
49  ???                                 0x0000000000000005 0x0 + 5

Thread 0 Crashed:
0   libsystem_kernel.dylib          0x00007fff5225a33a __pthread_kill + 10
1   libsystem_c.dylib               0x00007fff521e9b7c abort + 120
2   libc++abi.dylib                 0x00007fff500ef858 abort_message + 231
3   libc++abi.dylib                 0x00007fff500e0cbf demangling_terminate_handler() + 262
4   libobjc.A.dylib                 0x00007fff512a1c0b _objc_terminate() + 96
5   libc++abi.dylib                 0x00007fff500eec87 std::__terminate(void (*)()) + 8
6   libc++abi.dylib                 0x00007fff500eec29 std::terminate() + 41
7   libdispatch.dylib               0x00007fff520dc8df _dispatch_client_callout + 28
8   libdispatch.dylib               0x00007fff520ddb02 _dispatch_once_callout + 20
9   com.apple.UIKitCore             0x00007fff48d0caef +[UIButtonLabel _defaultAttributes] + 97
10  com.apple.UIKitCore             0x00007fff48d162a3 -[UIButton _deriveTitleRect:imageRect:fromContentRect:calculatePositionForEmptyTitle:] + 2312
11  com.apple.UIKitCore             0x00007fff48d15878 -[UIButton _titleRectForContentRect:calculatePositionForEmptyTitle:] + 287
12  com.apple.UIKitCore             0x00007fff48d1591f -[UIButton titleRectForContentRect:] + 49
13  com.apple.UIKitCore             0x00007fff48d1af14 -[UIButton _setupTitleViewRequestingLayout:] + 156
14  com.apple.UIKitCore             0x00007fff48d10e5f -[UIButton titleLabel] + 42
15  com.apple.Foundation            0x00007fff2590f8ad -[NSObject(NSkeyvalueCoding) valueForKey:] + 317
16  com.apple.Foundation            0x00007fff259105ae -[NSObject(NSkeyvalueCoding) setValue:forKeyPath:] + 251
17  com.apple.IBCocoaTouchToolFoundation    0x00000001066452e3 __IBApplyMarshalledState_block_invoke + 425
18  com.apple.IBCocoaTouchToolFoundation    0x0000000106644bc5 IBApplyMarshalledState + 512
19  com.apple.IBCocoaTouchToolFoundation    0x0000000106624c62 -[UIButton(IBCocoaTouchToolIntegration) initWithMarshalledValues:orderedKeys:ignoredKeys:globalMarshallingContext:] + 302
20  com.apple.IBCocoaTouchToolFoundation    0x0000000106645441 +[NSObject(IBCocoaTouchToolIntegration) instantiateWithMarshalledValues:orderedKeys:ignoredKeys:globalMarshallingContext:] + 118

如何避免这些代理崩溃?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。