text: Q:
  
  Casting subclass from base class where subclass has generic in Swift
  
  I have a base class type and a subclass type, where the subclass includes a generic type. If I have the subclass stored in the form of the base class type but I would like to type cast it back to the subclass type, Swift won't seem to let me.
  Below is an example of what I mean:
  class Base {
  
  }
  
  class Next<T> : Base where T : UIView {
      var view: T
      init(view: T) {
          self.view = view
      }
  }
  
  let a: [Base] = [Next(view: UIImageView()), Next(view: UILabel())]
  for item in a {
      if let _ = item as? Next {
          print("Hey!")
      }
  }
  
  Why is "Hey!" never printed?
  
  EDIT:
  "Hey!" is printed if the cast reads:
  if let _ item as? Next<UIImageView> ...
  
  but only for the case of the UIImageView class.
  and
  "Hey!" is printed if one of the items in the array a is:
  Next(view: UIView())
  
  Ideally, I would like to not know what type the generic is when casting, but I realise this may not be possible.
  
  A:
  
  The generic Next<T> is a template of sorts that creates unique separate classes. Next<UIView> and Next<UIImageView> are two completely unrelated types.
  You can unite them with a protocol:
  class Base {
  
  }
  
  class Next<T> : Base where T : UIView {
      var view: T
      init(view: T) {
          self.view = view
      }
  }
  
  protocol NextProtocol { }
  
  extension Next: NextProtocol { }
  
  let a: [Base] = [Next(view: UIImageView()), Next(view: UILabel()), Base()]
  for item in a {
      if item is NextProtocol {
          print("Hey!")
      } else {
          print("Only a Base")
      }
  }
  
  Output:
  
  Hey!
  Hey!
  Only a Base
  
  By defining a protocol such as NextProcotol that all classes derived from Next<T> conform to, you can refer to them as a group and distinguish them from other classes that derive from Base.
  Note: To check if an item is of a type, use is instead of checking if the conditional cast as? works.
  
  