ios - Alamofire:错误:无法将类型 'Result<_,_>' 的值分配给类型 'Result'

标签 ios swift swift2 alamofire

我正在使用 Alamofire用于发送请求和处理响应。我创建了一个使用 Alamofire.Result 的简单类输入:

class MyHandler {
    private var _result: Alamofire.Result

    init(result: Alamofire.Result) {
        //ERROR: Cannot assign value of type 'Result<_,_>' to type 'Result'
        self._result = result
    }
}   

我的代码中出现了如上所示的奇怪错误。这是 source codeAlamofire.Result .这是一个枚举 enum Result<Value, Error: ErrorType> .

我正在使用相同的 Alamofire.Result输入我的类(class) self._result & result通过初始值。

为什么会出现此错误?看起来编译器认为它们不是同一类型...我的 xcode 版本是 7.3.1。这是 xcode 错误吗?

最佳答案

AlamoFire.Result 是具有两个占位符的泛型

public enum Result<Value, Error: ErrorType> { ... }

您可以为具体类型声明您的类 占位符,例如

class MyHandler {
    private var _result: AlamoFire.Result<Int, NSError>

    init(result: AlamoFire.Result<Int, NSError>) {
        self._result = result
    }
}

但您更有可能想要声明一个泛型类:

class MyHandler<Value, Error: ErrorType> {
    private var _result: AlamoFire.Result<Value, Error>

    init(result: AlamoFire.Result<Value, Error>) {
        self._result = result
    }
}

关于ios - Alamofire:错误:无法将类型 'Result<_,_>' 的值分配给类型 'Result',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37195110/

相关文章:

swift - 为多个swift参数设置相同的值

ios - 将我的 nsstring 转换为 json

ios - UICollectionView,在不知道 NSIndexPath 的情况下滚动到项目

ios - iOS BLE 的断开时间

ios - 在 AppDelegate 的 UI Bootstrap 代码中选择 Root View Controller (与 viewDidLoad 相比)

ios - 如何在 swift 2.0 的 UITextfield 中只允许特定的一组数字

ios - 我们可以在iOS中部分隐藏UIView吗

ios - detailTextLabel 未显示,但已设置 100%

swift - 在 Swift 编程语言中使用可选项

swift - 如何在 Swift 中调用 backtrace_symbols()