Swift - 在switch语句中绑定泛型参数

时间:2016-09-28 18:46:05

标签: swift generics switch-statement

使用Enumerations with Switches时,可以将关联的值绑定到变量。例如:

let x: Int? = 5

switch x {
case .some(let wrapped):
    print(wrapped) //Assosiated value bound to "wrapped"
case .none:
    print("nil")
}

是否有类似的方法来绑定泛型类型参数?这是我正在寻找的语义类型:

func foo(_ arg: T) {
    switch arg {
    case array as Array<let X>:
        print("The argument is an array of \(X)")
    default:
        print("The argument is not an array")
    }
}

3 个答案:

答案 0 :(得分:2)

您想检查参数是否为Array?试试这个:

func foo<T>(arr: Array<T>) {
    print("argument is array of \(T.self)")

    switch T.self {
        case is Int.Type:
            print("integer array")
        default:
            print("Unknown type or empty")
    }
}

然后你可以只将数组作为参数传递,而无需进行无用的检查。

答案 1 :(得分:1)

您可以使用Mirror - 它的设计,而不是您正在寻找的语法,但如何:

func isItACollection(_ any: Any) -> [String : Any.Type]? {
    let m = Mirror(reflecting: any)
    switch m.displayStyle {
    case .some(.collection):
        // `.children` gives us the actual types of the elements
        // This may be heterogeneous, so we spoof a Set of types
        // by using a dictionary
        var types: [String: Any.Type] = [:]
        for (_, t) in m.children {
            types["\(type(of: t))"] = type(of: t)
        }
        return types
    default: 
        return nil
    }
}

func test(_ a: Any) -> String {
    switch isItACollection(a) {
    case .some(let X):
        return "The argument is an array of \(X)"
    default:
        return "The argument is not an array"
    }
}

test([1, 2, 3]) // The argument is an array of ["Int": Swift.Int]
test([1, 2, "3"]) // The argument is an array of ["Int": Swift.Int, "String": Swift.String]
test(["1", "2", "3"]) // The argument is an array of ["String": Swift.String]
test(Set<String>()) // The argument is not an array
test([1: 2, 3: 4]) // The argument is not an array
test((1, 2, 3)) // The argument is not an array
test(3) // The argument is not an array
test("3") // The argument is not an array
test(NSObject()) // The argument is not an array
test(NSArray(array:[1, 2, 3])) // The argument is an array of ["_SwiftTypePreservingNSNumber": _SwiftTypePreservingNSNumber]

答案 2 :(得分:0)

如果你只是想知道泛型类型,你可以这样做:

func checkType<T>(_ arg: Array<T>) {
    print("The argument is an Array of type \(type(of: T.self))")
}

func checkType<T>(_ arg: T) {
    print("The argument is of type \(type(of: T.self))")
}

checkType([1])
checkType(1)
// The argument is an Array of type Int.Type
// The argument is of type Int.Type

但是,如果您还想绑定泛型类型持有的值:

func showValue<T>(from arg: T) {
    switch arg {
    case let value as Array<Any>:
        print("value is \(value)")
    case let value as Int:
        print("value is \(value)")
    default:
        print("couldn't get value")
    }
}

showValue(from: [1])
showValue(from: 1)
// value is [1]
// value is 1