【问题标题】:Cannot invoke initializer for type 'Result<Any>' with an argument list of type '(int,int)' on Xcode 7.3.1无法在 Xcode 7.3.1 上使用类型为“(int,int)”的参数列表调用类型“Result<Any>”的初始化程序
【发布时间】:2016-05-10 08:54:55
【问题描述】:

在 Xcode 7.3.1 的操场上尝试这个会出现编译错误:

Cannot invoke initializer for type 'Result<Any>' with an argument list of type '(int,int)' 

在 Xcode 7.3 上它运行良好。这是代码:

import UIKit
public enum Result<T> {
    case Success(T)
    case Failure(ErrorType)
    public init(_ value: T) {
        self = .Success(value)
    }
    public init(_ error: ErrorType) {
        self = .Failure(error)
    }
}
func handleResult(result: Result<Any>) {
    switch result {
    case .Success(let value):
        print(value)
    case .Failure(let error):
        print(error)
    }
}
let b = Result<Any>((1,2))    //This doesn't work on Xcode 7.3.1
handleResult(b)

有什么想法吗? Xcode 7.3.1 编译器变得更严格了吗?

【问题讨论】:

    标签: swift generics tuples xcode7.3


    【解决方案1】:

    不确定编译器发生了什么变化,但恕我直言,无论如何你都应该放弃显式的 Result&lt;Any&gt; 并保持通用:

    func handleResult<T>(result: Result<T>) {
        switch result {
        case .Success(let value):
            print(value)
        case .Failure(let error):
            print(error)
        }
    }
    
    let b = Result((1,2))
    handleResult(b)
    

    【讨论】:

      【解决方案2】:

      Functions - Omitting External Parameters 可以帮助你理解我想。

       import UIKit
       public enum Result<T> {
          case Success(T)
          case Failure(ErrorType)
          public init(value: T) {
              self = .Success(value)
          }
          public init(error: ErrorType) {
              self = .Failure(error)
          }
      }
      
      func handleResult(result: Result<Any>) {
                  switch result {
                  case .Success(let value):
                      print(value)
                  case .Failure(let error):
                      print(error)
                  }
              }
      
      let b = Result<Any>(value: (1,2))    //This works only on Xcode 7.3.1
              handleResult(b)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多