[SOLVED] UINavigationController top view controller cannot be downcasted to ItemsViewController

In page 208, the UINavigationController top view controller is downcasted to an ItemsViewController. I am using Xcode 8.2.1 and Swift 3, and this downcast generated an error when building:

iOSProgramming5th/Homepwner/Homepwner/AppDelegate.swift:25:67: Use of undeclared type ‘I​t​e​m​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r’

the code:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    // Create an ItemStore
    let itemStore = ItemStore()
    // Access the ItemViewController and set its item store
    let navController = window!.rootViewController as! UINavigationController
    let itemsController = navController.topViewController as! I​t​e​m​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r
    itemsController.itemStore = itemStore
    return true

The code of previous chapter 12 without the navController was working fine. I checked several things, amongst them:

  • ItemsViewController.swift is included in the “Compile Sources” section and not included in “Copy Bundle Resources” section
  • ItemsViewController.swift target membership is Homeowner.

Any ideas or suggestions are welcome, because I have been for more than one hour struggling with this error.

Thanks in advance,

I found the solution. Xcode was adding “something” behind the final r in navController.topViewController as! I​t​e​m​s​V​i​e​w​C​o​n​t​r​o​l​l​e​r when using autocomplete or manually writing. I compared the previous chapter file with the current one using a comparison tool and found this difference. Copy and paste from the old file solves the problem. Hope this helps someone else.

It’s unfortunate that an IDE can get in your way like that, but I guess that’s the cost of being able to tab to certain positions when autocompleting: sometimes the invisible characters marking those spots don’t get deleted.