Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 无法调用类型为';结果<;任何>';具有类型为';(int,int)和"x27,;关于Xcode 7.3.1_Swift_Generics_Tuples_Xcode7.3 - Fatal编程技术网

Swift 无法调用类型为';结果<;任何>';具有类型为';(int,int)和"x27,;关于Xcode 7.3.1

Swift 无法调用类型为';结果<;任何>';具有类型为';(int,int)和"x27,;关于Xcode 7.3.1,swift,generics,tuples,xcode7.3,Swift,Generics,Tuples,Xcode7.3,在Xcode 7.3.1上的操场上尝试此操作会导致编译错误: Cannot invoke initializer for type 'Result<Any>' with an argument list of type '(int,int)' 无法使用类型为“(int,int)”的参数列表调用类型为“Result”的初始值设定项 但在Xcode 7.3上运行良好。代码如下: import UIKit public enum Result<T> { case

在Xcode 7.3.1上的操场上尝试此操作会导致编译错误:

Cannot invoke initializer for type 'Result<Any>' with an argument list of type '(int,int)' 
无法使用类型为“(int,int)”的参数列表调用类型为“Result”的初始值设定项
但在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)
导入UIKit
公共枚举结果{
成功案例(T)
案例失败(错误类型)
公共初始化(u值:T){
自我=.成功(价值)
}
公共初始化(\错误:ErrorType){
self=.Failure(错误)
}
}
func handleResult(结果:result){
切换结果{
成功案例(let value):
打印(值)
案例。失败(let错误):
打印(错误)
}
}
设b=Result((1,2))//这在Xcode 7.3.1上不起作用
handleResult(b)

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

不确定编译器发生了什么变化,但我认为您应该放弃显式
结果,并保持通用性:

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)
func handleResult(结果:结果){
切换结果{
成功案例(let value):
打印(值)
案例。失败(let错误):
打印(错误)
}
}
设b=结果((1,2))
handleResult(b)
可能会帮助您理解我的想法

 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)
导入UIKit
公共枚举结果{
成功案例(T)
案例失败(错误类型)
公共初始化(值:T){
自我=.成功(价值)
}
公共初始化(错误:ErrorType){
self=.Failure(错误)
}
}
func handleResult(结果:result){
切换结果{
成功案例(let value):
打印(值)
案例。失败(let错误):
打印(错误)
}
}
设b=Result(值:(1,2))//这仅适用于Xcode 7.3.1
handleResult(b)