wrong generic type in swift

2019-04-02 09:53发布

问题:

After run following code in playgroud, why x value is 2? Is there anything wrong with swift generic type and "is" operator? class Item {}

class Campaign: Item {}

class AdGroup : Item {}

class A<T: Item> {
    func val() -> Int{
        let item = T()
        if item is Campaign {
            return 1
        } else {
            return 2
        }
    }
}

var m = A<Campaign>()
let x = m.val()