Beginning Table Views · NSObject | Ray Wenderlich


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5995-beginning-table-views/lessons/41

In part 25 - dealing with random numbers:

When I code this:

let randomNumber = Int.random(in: 0 … titles.count - 1)

I get the following error: Type ‘Int’ has no member ‘random’

Any ideas as to what I’ve done wrong?

@rfwarnock This is a Swift 4.2 feature, so you have to install Xcode 10 to make it work. Please let me know if you have any other questions or issues. Thank you!

Hi! I have the answer: in the class we use the references, so when we use Checlist class in this part , where we throw item through edit segue:

 let item = todoList.todos[indexPath.row]
 addItemViewController.itemToEdit = item

and isn’t all this variable/constans reference to the same object? And when we change one of them all should change? I mean in AddItemTableViewController.swift we used this in func done:

if let item = itemToEdit, let text = textField.text {
     item.text = text
     delegate?.addItemViewController(self, didFinishEditing: item)
 }

All this variable and constants in ChecklistViewController.swift and AddItemTableViewController.swift referenced to the same object, we didn’t create any new Checklist item, so why we should search for this object in todoList.todos array and change it manually?
Is this the case that references works only in scope of Program.swift file?

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

Yes, you are using the same reference. In fact, you don’t need to do anything. But by passing the reference back, you know which item has changed in case you want to do some animating or visually show the user. I hope that helps!

Very elegant solution with NSObject subclassing - but I don’t understand how it works. I was expecting to have a defect if I had 2 tasks with the same name and checkmark status, but that’s not the case. Where can I read more on how this works?

Thanks! Great tutorial btw!

@bdmoakley Do you have any feedback about this? Thank you - much appreciated! :]

Thanks el_cid. It sounds like you are getting confused with object equality. When you create a new object, you are essentially setting aside memory for it. When you create another object, you are setting another space in memory for a new object.

Both objects can contain the exact same name and status, but they are not equal because they are two different objects because they share different parts of memory. If you have two references pointing towards the same object, then they would be the same.

I hope this helps! Ping me if you are still confused. Cheers!

Dear Brian,

I would like to know if the subclassing of NSObject could be avoided with the new Comparable conformance introduced “recently” (don’t know exactly when).

Also, when you subclass to NSObject your ChecklistItem class doesn’t have the generatedTitle method anymore. Is this intentional?
In any case it was not explained.

Thank you so much for speaking slowly :slight_smile: It is very clear!

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi NotationMaster,

Yes, you could easily implement the required protocols and if this was my own app, this is most likely the approach I would use. But in this case, it provides a nice introduction to NSObject and the benefits it brings to the table. In the Objective C days, everything was an NSObject.

I don’t remember adding a generatedTitle to the ChecklistItem (it’s been almost 9 months since I wrote this) - where did you last see it?

Cheers!

Hi, when I code this (in the extension of the ChecklistViewController, addItemViewController - method):
todoList.todos.index(of: item)
I get the warning that the method index(of: item) is deprecated and I should use todoList.todos.firstIndex(of: item). Is there any major difference between these two methods?

Thank you :blush:

Nope, it is basically the same function, renamed to more precisely describe what it returns.

1 Like

Ah, okay. Thank you so much^^

Hi - I’m slightly confused about why we’re getting the error message when adding the new checklist item. When we first implemented this method of adding in the Segue video, why did it work and not return an error?

@bdmoakley Can you please help with this when you get a chance? Thank you - much appreciated! :]

The app was much simpler back then. Now you are repurposing one view controller to do both adding and editing.

I am running into the same issue. When I edit and click “Done” the app crashes stating a very similar error as we ran into creating it. “Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).’”

That’s definitely a bug. Whenever you run into that problem, it means your table is out of sync with your model. For instance, you may be telling your table that you have 5 rows when you only have four. I can’t diagnose the code problem without seeing code so your best bet is to compare your code with the code included with the complete project.