简体   繁体   English

Swift类扩展-无法识别的选择器发送到实例

[英]Swift Class Extension - unrecognized selector sent to instance

When creating a class extension on a foundation class I get a unrecognized selector sent to instance when using the following: 在基础类上创建类扩展时,使用以下命令时,我将unrecognized selector sent to instance

extension NSURLSessionDataTask: Taskable {

  func isRunning() -> Bool {
    return state == .Running
  }

}

Error: 错误:

[__NSCFLocalDataTask isRunning]: unrecognized selector sent to instance 0x7fa93bdc07d0

I am trying to create a common task interface so I am able to use different kinds of classes such as operations and session tasks, is there something I am missing here? 我正在尝试创建一个通用的任务界面,以便能够使用不同种类的类,例如操作和会话任务,这里我缺少什么吗?

Many of Cocoa classes are "class clusters", meaning you may get a different one from what you asked for. 许多可可类都是“类集群”,这意味着您可能会得到与您所要求的不同的类。 NSCFLocalDataTask is a private class and looks like it doesn't inherit from NSURLSessionDataTask . NSCFLocalDataTask是一个私有类,看起来它没有继承自NSURLSessionDataTask Try moving your extension to NSURLSessionTask . 尝试将扩展名移至NSURLSessionTask

Why this might happen: Objective-C BOOL properties starting with "is" may be handled differently. 为什么会发生:以“ is”开头的Objective-C BOOL属性可能会以不同的方式处理。 It may be that your func is exported to Objective-C as the "running" method, not as "isRunning". 可能是您的func作为“运行”方法而不是“ isRunning”导出到了Objective-C。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM