# Beginning Table Views · NSObject | Ray Wenderlich

**URL:** <https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059>\
**Category:** 🎓 Courses & Articles\
**Created:** [September 3, 2018, 1:43am UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059 "2018-09-03T01:43:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdmoakley](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bdmoakley/32/108772_2.png) [@bdmoakley](https://forums.kodeco.com/u/bdmoakley)\
**Post date:** [September 3, 2018, 1:43am UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/1 "2018-09-03T01:43:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rfwarnock](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@rfwarnock](https://forums.kodeco.com/u/rfwarnock)\
**Post date:** [September 3, 2018, 3:54pm UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/2 "2018-09-03T15:54:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [September 3, 2018, 4:03pm UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/3 "2018-09-03T16:03:46Z")

</div>

@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!

---

<div class="post-metadata">

**Author:** ![fames42](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/fames42/32/81494_2.png) [@fames42](https://forums.kodeco.com/u/fames42)\
**Post date:** [October 26, 2018, 10:07am UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/4 "2018-10-26T10:07:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [October 31, 2018, 2:09am UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/5 "2018-10-31T02:09:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bdmoakley](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bdmoakley/32/108772_2.png) [@bdmoakley](https://forums.kodeco.com/u/bdmoakley)\
**Post date:** [October 31, 2018, 3:10pm UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/6 "2018-10-31T15:10:26Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![el\_cid](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/el_cid/32/99140_2.png) [@el\_cid](https://forums.kodeco.com/u/el_cid)\
**Post date:** [October 31, 2018, 4:33pm UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/7 "2018-10-31T16:33:30Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [November 1, 2018, 1:14pm UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/8 "2018-11-01T13:14:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bdmoakley](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bdmoakley/32/108772_2.png) [@bdmoakley](https://forums.kodeco.com/u/bdmoakley)\
**Post date:** [November 2, 2018, 4:30pm UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/9 "2018-11-02T16:30:31Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![notationmaster](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/notationmaster/32/103764_2.png) [@notationmaster](https://forums.kodeco.com/u/notationmaster)\
**Post date:** [February 22, 2019, 9:58pm UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/11 "2019-02-22T21:58:29Z")

</div>

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 🙂 It is very clear!

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [February 25, 2019, 9:54pm UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/12 "2019-02-25T21:54:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bdmoakley](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bdmoakley/32/108772_2.png) [@bdmoakley](https://forums.kodeco.com/u/bdmoakley)\
**Post date:** [February 28, 2019, 4:24pm UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/13 "2019-02-28T16:24:27Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![eszter](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/eszter/32/81494_2.png) [@eszter](https://forums.kodeco.com/u/eszter)\
**Post date:** [May 5, 2019, 7:45pm UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/14 "2019-05-05T19:45:29Z")

</div>

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 😊

---

<div class="post-metadata">

**Author:** ![sgerrard](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/sgerrard/32/93812_2.png) [@sgerrard](https://forums.kodeco.com/u/sgerrard)\
**Post date:** [May 5, 2019, 9:55pm UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/15 "2019-05-05T21:55:14Z")

</div>

> [@eszter](#):
>
> Is there any major difference between these two methods?

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

---

<div class="post-metadata">

**Author:** ![eszter](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/eszter/32/81494_2.png) [@eszter](https://forums.kodeco.com/u/eszter)\
**Post date:** [May 6, 2019, 6:48pm UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/16 "2019-05-06T18:48:04Z")

</div>

Ah, okay. Thank you so much^^

---

<div class="post-metadata">

**Author:** ![saikiranc94](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/saikiranc94/32/81494_2.png) [@saikiranc94](https://forums.kodeco.com/u/saikiranc94)\
**Post date:** [August 30, 2019, 12:53am UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/17 "2019-08-30T00:53:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [September 6, 2019, 5:39am UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/18 "2019-09-06T05:39:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bdmoakley](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bdmoakley/32/108772_2.png) [@bdmoakley](https://forums.kodeco.com/u/bdmoakley)\
**Post date:** [September 17, 2019, 5:00pm UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/19 "2019-09-17T17:00:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kinneykare](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kinneykare/32/103971_2.png) [@kinneykare](https://forums.kodeco.com/u/kinneykare)\
**Post date:** [September 18, 2019, 10:12am UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/20 "2019-09-18T10:12:47Z")

</div>

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).’”

---

<div class="post-metadata">

**Author:** ![bdmoakley](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bdmoakley/32/108772_2.png) [@bdmoakley](https://forums.kodeco.com/u/bdmoakley)\
**Post date:** [September 24, 2019, 1:55pm UTC](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059/21 "2019-09-24T13:55:23Z")

</div>

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.

[Next page](https://forums.kodeco.com/t/beginning-table-views-nsobject-ray-wenderlich/48059.md?page=2)
