I'm Lost with all the Errors that are displaying

#1

It seems like in chapter 10, they are going the long way around to build tables to me.
I am trying to follow along entering the code like a good little programming robot, but all these red warning errors are popping up. I will attach a screen shot so you will see what I’m talking about.

I have tried to go through this section for so long it is becoming a real chore to keep going. I am about to give up on this whole book, because it is truly not for folks like me who are just beginning to learn iOS programming.

#2

Don’t despair.

Have faith in the book.

And most importantly, make sure that you use the version of Xcode for which the book was written.

Also post your actual code, rather than only a screen shot of it.

#3

Sorry, I see the screenshot doesn’t show much…
and the Xcode I am using is Version 8.2.1 (8C1002)

I know about that because I ran into that problem with the previous version of this book that I bought and could not complete the examples because of the version changes. So I have purposely not updated my Xcode until I finish this book.

Okay, So here is the code where I am having errors show up:

AppDelegate.swift - Shows an error on ItemsViewController as Use of undeclared type "ItemsViewController"
CODE>
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    //Create an ItemStore
    let itemStore = ItemStore()
    
    // Access the ItemsViewController and set its item store
    let itemsController = window!.rootViewController as! ItemsViewController
    itemsController.itemStore = itemStore
    
    return true
}

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

}

ItemsViewController.swift - Shows two errors, ItemStore! has error as - undeclared type, and let cell = UITableViewCell(style: .value1, resuseIdentifier: “UITableViewCell”) has error as -= Argument labels (style: resuseIdentifier:) do not match any available overloads.

CODE>
import UIKit

class ItemsViewController: UITableViewController {

var itemStore: ItemStore!

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return itemStore.allItems.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // Create an instance of UITableViewCel, with default appearance 
    let cell = UITableViewCell(style: .value1, resuseIdentifier: "UITableViewCell")
    
    // Set the text on the cell with the description of the item
    // that is at the nth index of items, where n = row this cell
    // will appear in on the tableview
    let item = itemStore.allItems[indexPath.row]
    
    cell.textLabel?.text = item.name
    cell.detailTextLabel?.text = "$](item.valueInDollars)"
    
    return cell
}

}

#4

1.0 It may be that the file containing the ItemsViewController class is not part of the project you are working with.

Make sure that that file is in the project.

2.0 Similar issue as in point 1.0 above.

3.0 For this, check the declarations of initializers for UITableViewCell. Put the cursor on UITableViewCell; then either command-click to check the declarations as seen by the compiler or option-click to check the documentation.

After this if you are still stuck, zip up your project and send it to me if you would like me to have a closer look.

#5

You were correct. Three of the swift files were not in the project folder, even though I created them from within the project. So I will have to be more careful on that next time.

And on 3.0 it was a misspelled style: I had resuseIdentifier: instead of reusedIdentifier: , in the Argument labels section.

Thanks for your help.

#6

I still cannot get this section to Build and Run.

I am now getting an error in the AppDelegate.swift file.

The error I am getting is Thread 1: Signal SIGABRT
on the line that has
let itemsController = window!.rootViewController as! ItemsViewController

Below is the code for it:

//
// AppDelegate.swift
// Homepwner
//
// Created by Warrior2Mac on 11/19/17.
// Copyright © 2017 Mellenware. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    //Create an ItemStore
    let itemStore = ItemStore()
    
    // Access the ItemsViewController and set its item store
    let itemsController = window!.rootViewController as! ItemsViewController
    itemsController.itemStore = itemStore
    
    return true
}

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

}

#7

After checking that window contains a reference to a UIWindow object, check to see if the rootViewController really contains a reference to an ItemsViewController object.

For example:

guard let window = window else {
   // window is not set
   return
}

guard let itemsController = window.rootViewController as? ItemsViewController else {
   // rootViewController is not an ItemsViewController
   return
}

#8

I couldn’t really understand what you wanted me to do with the code you sent.
However, here is the output I get when I try to compile the code with the previously stated error.

2017-12-28 10:07:30.155 Homepwner[1591:394535] Unknown class _TtC16HomepwnerUITests19ItemsViewController in Interface Builder file.
Could not cast value of type ‘UITableViewController’ (0x10acd4f58) to ‘Homepwner.ItemsViewController’ (0x10907b780).
(lldb)

I went through every single line of code like a “Good Little Code Monkey” and I have everything exactly as it states in the book. My problem is, they don’t show you the whole code, only pieces of it at a time. So I don’t know if some code is in the right places. Does it go within the brackets, is it outside of the brackets… etc. I am wasting more time than I am learning.

#9

My apologies for not being more explicit.

Simply replace the following line of code:

let itemsController = window!.rootViewController as! ItemsViewController

With this:

guard let window = window else {
   // window is not set (highly unlikely)
   print ("--> window is not set (highly unlikely)")
   return
}

guard let itemsController = window.rootViewController as? ItemsViewController else {
   // rootViewController is not an ItemsViewController
   print ("-->  rootViewController is not an ItemsViewController")
   return
}

The above code ensures that window variable contains a window object; and rootViewController contains an ItemsViewController object.

Try the above code to see if the second guard statement fails. If it does, then most likely your are not following the book’s instructions carefully. Start with a clean slate: create a new project; do not copy and paste code from the old project; type in code exactly as the book says.

The book assumes that you already know how to write code, and that you should be able to connect the dots without being spoon-fed all the way through examples.

#10

ibex10 , Thanks for all your assistance. The trouble shooting code did not work it and gave other errors and would not compile the code. So I did the most drastic approach and started again from scratch, because I would have otherwise gotten nowhere. But, whatever I did to screw it up, it now works as it should and I am currently beginning in chapter 12 of the book today.

#11

I am happy to hear that you have overcome the hurdle and started a new chapter. Godspeed.

#12

I got the same “SIGABRT” error on the line
// let itemsController = window!.rootViewController as! ItemsViewController

my problem was that I didn’t declare the class on the main story board

54%20PM