0

Maybe it's just too early for now but I got a little piece of code I can't follow.

In a UITableViewController is the following

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = printTable.dequeueReusableCellWithIdentifier("printCell", forIndexPath: indexPath) as UITableViewCell
    configureTestCell(cell, atIndexPath: indexPath)

    return cell;
}

The configureTestCell function:

func configureTestCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath)
{
    let printCell = cell as PrintCell

    if (self.searchActive)
    {
        printCell.nameLabel.text = "Project \(filteredData[indexPath.item])"
    }
    else
    {
        printCell.nameLabel.text = "Project \(printData[indexPath.item])"
    }
}

So my problem and question here is, why are the changes made in printCell have an effect on the cell object in the tableView function? Isn't the cell just a copy or am I missing something stupid-easy?

Nitro.de
  • 821
  • 10
  • 27

4 Answers4

2

I am not sure but i think is because UITableViewCell is a class. It always passed by reference. You are passing a pointer of the class to the function.

0

In Swift classes are 'reference types' so when you pass an object of a class to a function it does not create a new copy of it but rather creates and new reference to the object you have.

This makes it possible to modify the referenced object.

Abdullah
  • 7,143
  • 6
  • 25
  • 41
  • so how can i make a copy of a the class object then? – Nitro.de Apr 02 '15 at 08:13
  • You have to create a new object and manually copy all the data from your source object to it. You could check these two questions for more info http://stackoverflow.com/questions/24242629/implementing-copy-in-swift and http://stackoverflow.com/questions/24754559/how-to-do-deep-copy-in-swift – Abdullah Apr 02 '15 at 09:05
0

For objects we can pass it by reference and by value. When you write

let printCell = cell as PrintCell

Its by reference, means printCell will not have new memory allocation, it will point to memory location of cell itself.

Now when you perform operation of printCell it will be reflected in your view, As the both objects point to the same memory location.

If you assign object by value using copy keyword (I don't know how to do it with swift as I am following objective-c).

It will have new memory location and when you perform any task on printCell it will not reflected in Tableview.

Samir
  • 902
  • 9
  • 23
  • I know that parameters can be passed by reference and/or value, just did not know that class objects are standard passed by reference. However ty for explaining that ;) – Nitro.de Apr 02 '15 at 08:15
  • For objects when you are not using copy () method while assigning its, by reference. let printCell = cell.copy() Try this to copy in swift, I guess its correct. – Samir Apr 02 '15 at 08:18
  • cell.copy() as UITableViewCell gives me NSInvalidArgumentException – Nitro.de Apr 02 '15 at 08:25
  • Try to cast the type like let printCell = cell.copy() as PrintCell Or Ask who knew how to do it in swift, As I am not programming in swift i can just help logically. :) – Samir Apr 02 '15 at 08:28
  • same exception withprintcell cast ;) however thank you, i'll make a new question for the pass by value thing – Nitro.de Apr 02 '15 at 08:29
  • I've just started a new question for that, if you're interested http://stackoverflow.com/questions/29408486/how-to-pass-class-objects-by-value – Nitro.de Apr 02 '15 at 08:35
0

When you are passing a object to a function in Swift it is passed by reference. That mean you are passing a pointer to the object. In the following function

func configureTestCell(cell: UITableViewCell, atIndexPath indexPath: NSIndexPath)

cell is an instance of type UITableViewCell so it is passed by refernce type. The "cell" in "tableView" function and "printCell" in "configureTestCell" are pointing to the same object.

Nitheesh George
  • 1,357
  • 10
  • 13