简体   繁体   English

如何测试另一个快速通用类型?

[英]How to test a swift generic type another one?

I have some generic type class but no instance of object to test. 我有一些通用类型类,但没有要测试的对象实例。 What I would like to do is to alter the behavior of the function according to the runtime type. 我想做的是根据运行时类型更改函数的行为。

class MyGenericUtility<SomeGenericClass> {

    func myFunction() {
        // so far I have tested "is", "==" and "==="
        if SomeGenericClass is SomeRealClass {
            println("some special stuff there")
        }
        println("some generic stuff as the name tells")
    }

}

You can compare the class type, using SomeGenericClass.self == SomeRealClass.self as, 您可以使用SomeGenericClass.self == SomeRealClass.self来比较类类型,

class MyGenericUtility<SomeGenericClass> {
    func myFunction() {
        if SomeGenericClass.self == SomeRealClass.self {
            print("SomeRealClass stuffs")
        } else if SomeGenericClass.self == String.self {
            print("String stuffs")
        }
    } 
}


let someRealUtility = MyGenericUtility<SomeRealClass>()
someRealUtility.myFunction()

let stringUtility = MyGenericUtility<String>()
stringUtility.myFunction()

Rather than testing at runtime, you should generally handle this at compile time with constrained extensions (this assumes Swift 2). 而不是在运行时进行测试,通常应该在编译时使用受约束的扩展来处理此问题(假定使用Swift 2)。 Doing it this way avoids any need to do unsafe as! 这样可以避免做任何不安全的事情as! casting when you need to access type-specific parts of the instance. 需要访问实例的特定于类型的部分时进行强制转换。

class MyGenericUtility<SomeGenericClass> {
}

// Special handling for `SomeRealClass`
extension MyGenericUtility where SomeGenericClass: SomeRealClass {
    func myFunction() {
        print("SomeRealClass stuffs")
    }
}

// Default handling for any unspecified class
extension MyGenericUtility {
    func myFunction() {
        print("Other stuffs")
    }
}

let someRealUtility = MyGenericUtility<SomeRealClass>()
someRealUtility.myFunction()

let stringUtility = MyGenericUtility<String>()
stringUtility.myFunction()

Note that this is based on inheritance, not equality, so any subclass of SomeRealClass would get the SomeRealClass behavior. 请注意,这是基于继承而不是相等性,因此SomeRealClass任何子类SomeRealClass将获得SomeRealClass行为。

You can't use the generic type directly, you need to use a property of that type when comparing with "is". 您不能直接使用通用类型,与“ is”进行比较时需要使用该类型的属性。

class MyGenericUtility<T> {
    var a: T

    func myFunction() {
        if a is Int {
            println("some special stuff there")
        }
        println("some generic stuff as the name tells")
    }

    init(value: T) {
        a = value
    }
}

let test = MyGenericUtility(value: 5)
test.myFunction()  
// Output: some special stuff there
//         some generic stuff as the name tells

let test2 = MyGenericUtility(value: "foo")
test2.myFunction()
// Output: some generic stuff as the name tells

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM