Swift, could not cast value of type

Try this:

  • Open LibraryCollectionViewController.swift
  • Press Option + Command + 1 to open File Inspector
  • Find Target Membership section
  • Check MyApp

My problem was I was referencing NSBundle.mainBundle() from the unit tests whereas I needed to use:

let bundle = Bundle(for: self.classForCoder)

This can happen if a class is a member of two targets (maybe two frameworks?) - and your app is somehow referencing one framework while the unit test target is referencing the second target.

So one of the compiled artefacts has a fully-qualified type Framework1.MyClass while the other is Framework2.MyClass - and they're different classes at runtime because they have different namespaces. (Even though both classes are compiled from the same Swift source file)

Tags:

Ios

Swift