有UIButton
@objc class OpenCloseButton: UIButton {
/* used NSNumber due to "-swift.h" collaboration file do not generate primitive types.
Hence, cannot use `Bool` */
var isOpen: Bool = 0
}
现在,我将UIButton
放入故事板并将其更改为OpenCloseButton
并将以下操作连接到该按钮:
@IBAction func openCloseDetails(_ button: OpenCloseButton) {
if button.isOpen == false { // here it crashes
button.isOpen = true
self.closeDetails()
} else {
button.isOpen = false
self.openDetails()
}
}
当App运行时,当我用
点击该按钮时它会崩溃 Threas: 1 EXC_BAD_ACCESS (code=2, address=0x06549gfr)
异常断点显示方法第二行的崩溃。
我在这里缺少什么?
其他信息:
控制台在崩溃线上有以下输出:
(lldb) po button
▿ Optional<OpenCloseButton>
(lldb) po button!
<UIButton: 0x146059c0; frame = (728 12; 32 32); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x1469ca40>>
答案 0 :(得分:2)
您可以在致电if button.isOpen?.boolValue...
之前添加此内容:
guard let button = button as? OpenCloseButton else {
return
}
仅用于测试目的。是继续运行还是在方法中调用返回值?