Objective-C interoperability type mismatch

NOTE: This question is not about Apples frameworks, I will just use it as an example to make my point clear.

When using Apples MapKit I came across a bug. At least I’m 99% sure it’s a bug. The MapViewDelegate has the function did add views.

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView])

When using a map view with cluster annotations this function will get called with a wrong typed parameter. This will cause a crash, since the types do not match.

Fatal error: NSArray element failed to match the Swift Array Element type

A minimal implementation will cause the error:

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
    print(views) // Crash here
}

In Objective-C type checking wouldn’t be an issue at all. As soon, as I start using the parameter “views”, it will cause a crash. And I totally understand that. Since Objective-C does not have strong typing, it is possible to pass a different typed object to the function. And Objective-C interoperability is a part of swift. So there should be (or probably is) a good way, to double check that.

How should I deal with something like that? Is that even possible?

// This won't do anything. I guess the compiler will simply remove it.
guard views is [MKAnnotationView] else { return }
// This won't fix it either. The compiler will perform optimization here too.
guard let castedViews = views as? [MKAnnotationView] else { return }
// This will fix the problem
guard let castedViews = (views as [NSObject]) as? [MKAnnotationView] else { return }

The the solution adds a “safe” check to my implementation that won’t be optimized by the compiler. However it’s probably just a matter of time until the compiler will perform optimizations there too.

  1. Is there any way to fix a problem like that (reliably)? (With Objective-C it will be always possible)
  2. Shouldn’t swift check if the type matches before code within the body can be executed?
  3. Where should I start to learn more about how the compiler works in scenarios like these?