Group Group Group Group Group Group Group Group Group

Beginning Table Views · NSObject | Ray Wenderlich

#1

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

#2

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?

0 Likes

#3

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

0 Likes

#4

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?

0 Likes

#5

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

0 Likes

#6

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!

0 Likes

#7

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!

0 Likes

#8

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

0 Likes

#9

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!

0 Likes

#11

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!

0 Likes

#12

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

0 Likes

#13

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!

0 Likes