ios - 无法将 UIButton 向下转换为子类

标签 ios swift cocoa-touch uibutton

我四处寻找,但无法弄清楚我做错了什么。我试图在以下代码行中将 UIButton 向下转换为子类:

var currentButton: FileButton = UIButton(type: .System) as! FileButton

FileButtonUIButton 的一个简单子(monad)类,它只存储几个与按钮相关的变量。代码如下:

import UIKit

class FileButton: UIButton {

     var fileAddress: String = ""
     var fileMsgId: String = ""
     var fileAppId: String = ""

}

当我尝试执行此代码时,控制台中出现以下错误:

Could not cast value of type 'UIButton' (0x19e9c9e40) to 'CollectionFun.FileButton' (0x100127490).

有什么想法吗?这看起来应该很简单,但我无法弄清楚。谢谢。

最佳答案

只需调用:

var currentButton: FileButton = FileButton(type: .System)

关于ios - 无法将 UIButton 向下转换为子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33833683/

相关文章:

用于解压缩文件的 iOS native 类或库?

ios - 在 Mapbox (iOS) 上将方向箭头添加到当前位置

ios - 类型 skproduct 的无效转换类型 nsmutablearray - ios

ios - 为什么 View 会自动设置动画?

ios - XCDYouTubeClient 未在 Swift 4 中播放视频

swift - 为什么以及何时可以在 Swift 中使用更短的方法签名?

iOS 11 双击图像会关闭 UIImagePickerController 和演示者 View Controller

iphone - 如何诊断 KERN_PROTECTION_FAILURE

ios - 使用自动布局约束时如何获取 View 的当前宽度和高度?

ios - Swift 项目因线程 1 : EXC_BAD_ACCESS (code = 1, 地址 = 0x0 而崩溃)