Chapter 20, page 813, Display the new IDs

When I run the app at this point I get -1 ID repeated for each new item I add instead of the new item starting at 0 and adding 1 for each additional ID like its supposed to.

I’ve looked my code over tons of times, compared it to the source code, and even read that section of the book again to make sure I didn’t miss anything. I’m sort of at a loss right now because I don’t want to continue if I’m not sure this portion of the app is correct.

When I ran the app from the source code it also seemed a little strange that the IDs displayed for new items started at 2 instead of 0 which kind of confuses me a bit.

Any help would be greatly appreciated,

Thank you

It is really just three spots you need to check. Make sure they are identical, including capitalization, especially in the quoted strings.

In DataModel.swift:

class func nextChecklistItemID() -> Int {
  let userDefaults = UserDefaults.standard
  let itemID = userDefaults.integer(forKey: "ChecklistItemID")
  userDefaults.set(itemID + 1, forKey: "ChecklistItemID")
  return itemID

In ChecklistItem.swift:

override init() {
  itemID = DataModel.nextChecklistItemID()

In ChecklistViewController.swift:

func configureText(for cell: UITableViewCell,
                  with item: ChecklistItem) {
  let label = cell.viewWithTag(1000) as! UILabel
  //label.text = item.text
  label.text = "\(item.itemID): \(item.text)"

Thank you for the response. I checked all of that code again with the code you provided and it is still doing the same thing. I even copied and pasted the code directly in to make sure I wasn’t overlooking any typos.

On page 807 it told me to add a var itemID = -1 property to ChecklistItem.swift I feel like that variable isn’t being updated for some reason and thats why I keep getting -1 repeated for each new item added. Please correct me if I’m wrong and if not how do I solve this issue?

On page 786 there was an exercise…

Exercise: Give ChecklistItem an init(text:) method that is used instead of the parameter-less init(). Or how about an init(text:checked:) method?”

So I did that. Now on page 807 it told me to add 3 more properties to ChecklistItem. Do those 3 additional items also need to be added to the init method I created earlier? I also added the override init() so now I have and override init() and an init. Not sure if something there is causing the issue?

This is what I have…

import Foundation
import UserNotifications

class ChecklistItem: NSObject, Codable {
var text = “”
var checked = false
var dueDate = Date()
var shouldRemind = false
var itemID = -1

override init() {
itemID = DataModel.nextChecklistItemID()

init(text: String, checked: Bool) {
self.text = text
self.checked = checked

func toggleChecked() {
checked = !checked

The second block above is an override of init in the ChecklistItem class definition.
That means it should run each time a new instance of ChecklistItem is created. It includes this line:

  itemID = DataModel.nextChecklistItemID()

which is setting the value of the var itemID.

If you tap that line just to the left of the line number in your ChecklistItem.swift file, it will set a breakpoint on it. Then run the app, and add a new checklist item. The code should stop on that line, and the code view will be displayed. If that doesn’t happen, then something is wrong with class definitions or something.

Yea I just set the breakpoint, nothing happened…

Oh wait, I just saw this. If you have two init methods like that, you would have to add the itemID = … code line to both of them. Which one gets called depends on whether you pass in text and a boolean, or just do a plain ChecklistItem(). If your add code is passing in text, that’s why it is not calling the other init and itemID is not getting initialized.

1 Like

You fixed it, thank you! Not sure if I would have figured that one out on my own :sweat_smile:

1 Like

I have another question along these lines…

The itemID’s are displaying correctly they start at 0 and count their way up each time a new item is added. However, if I delete any or all items the itemID never gets lower or zeros back out. I know the ID’s can go up to the billions but how come they don’t reset themselves to keep the number low? Why have an ID that’s potentially 500 when I only have 4 items? Is this a normal approach while creating real world applications?

That never happens. If you see that happen in some other app, it means somebody programmed it to do that. You could too; you would need to pick out the largest ID currently in use, and store that in the user defaults as the value for “ChecklistItemID.” Then the other code would start incrementing from that value.

It is usually better not to reuse IDs, if you don’t have a practical reason to. One day, when you are synching your phone with your iPad, it will mysteriously overwrite an item, and you will wonder why. If no one ever sees them, just let the number go up. I sometimes start with a larger number in the first place, like 10000, just to keep the number of digits the same.

1 Like

Oh ok, just seemed weird to me that even when I deleted all of the items then added new ones the ID’s continued counting from where the deleted ones left off. Probably no user would be adding billions of checklist items but could there ever be a point that the count is so high it would cause a problem with the app?

It is just doing what you the programmer told it to do. You save the last number you used in the user defaults, and when a new item is created, you retrieve that number, add 1 to it, and that is the new ID. Then you save that new ID as the last number used. You are in control of it, and it won’t do anything you don’t tell it to do.

An integer, in a 64 bit system, is stored as 8 bytes. That’s true whether it is 0 or 1 billion or something in between. So how high the number gets isn’t going to affect anything. You might find that an ID of 123456789 didn’t fit in the label if you didn’t make it wide enough, but that is the only sort of affect it could have.

This topic was automatically closed after 166 days. New replies are no longer allowed.