2

There is a base class with generics type:

class A<T: AnyObject> {}

and a sub class

class B: A<NSString> {}

and an instance of the sub class:

var b = B()

The question is: Why

b is A             // return false
b is A<AnyObject>  // return false
Grimxn
  • 22,115
  • 10
  • 72
  • 85
chk58
  • 21
  • 1
  • 1
    Are your sure that `b is A` returns `false`? It fails to compile in my test project. – Martin R Sep 09 '16 at 06:51
  • 1
    b is type of `A` not `A`. – Ozgur Vatansever Sep 09 '16 at 06:53
  • 1
    `A` and `A` are *unrelated* types. Compare http://stackoverflow.com/a/28118440/1187415 or http://stackoverflow.com/a/37190416/1187415 for similar issues with explanations. – Are you asking just out of curiosity or what do you want to achieve? – Martin R Sep 09 '16 at 08:08
  • 1
    As @MartinR says, `b is A` doesn't compile - mainly because you need to supply a type. If you do, e.g. `b is A` you get the right answer: `true`. As @ozgur says, you shouldn't be surprised at `b is A` being `false` - it isn't the same type. – Grimxn Sep 09 '16 at 08:11
  • yes, "b is A" has a compile error..... – chk58 Sep 09 '16 at 08:40
  • I need to have a array to store all the sub instance of A, – chk58 Sep 09 '16 at 08:40

0 Answers0