ios - 如何将实例方法作为闭包参数传​​递给另一个函数?

标签 ios swift

我正在尝试将一个对象方法作为参数传递给另一个期望具有相同签名的闭包的函数。我看过this没有任何成功。

// these are just dummy protocols to make my example run in Playground without include Alamofire or SwiftyJSON
public struct Response<Value, Error: ErrorType> { }
public protocol SwiftyJSONSerializable { }
public protocol ResponsePathProvider { }


class MyClass1 {
    func responseObject<T:SwiftyJSONSerializable>(keyPath path:String, completionHandler: Response<[String:T], NSError> -> Void) -> Self {
        return self
    }
}

class MyClass2 {
    func test() {
        let mc = MyClass1()
        mc.responseObject(keyPath: "/*/", completionHandler: testable) //fails
        mc.responseObject(keyPath: "/*/", completionHandler: self.dynamicType.testable(self)) //fails
    }

    func testable<T:SwiftyJSONSerializable>(response: Response<[String:T], NSError> -> Void) {
        //NOOP
    }
}

最佳答案

首先,可测试的功能应该是:

func testable<T:SwiftyJSONSerializable>(response: Response<[String:T], NSError>) {
    //NOOP
}

正确遵守类型。

不幸的是,我认为这要么是泛型与协议(protocol)相关的当前限制,要么是一个错误。如果您只是将 SwiftyJSONSerializable 更改为一个类,它就可以工作。

关于ios - 如何将实例方法作为闭包参数传​​递给另一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35905119/

相关文章:

ios - 从 native 应用程序启动时,Apple map 显示 "directions not available"

iphone - 在 iPhone 上使用图像和相机时出现内存问题

ios - 从自定义 Tableview 部分标题获取 indexPath

ios - iOS9 中的 KVO Collection View 内容插图

ios - 当用户关闭应用程序且任务仍处于事件状态时,NSURLSessionDownloadTask 不会删除文件

ios - 如何从分组条形图切换到简单条形图

Swift MacOS SpriteKit rightMouseDragged 不起作用

ios - 让 iAd 出现在所有场景中的相同状态、相同位置

ios - Core Data merge By Property Store Trump Merge Policy Type,什么意思?

由于 openldap 和 openssl makefile 错误,iOS 应用存档失败