-1

I have a custom class "Book" and I am trying to save two books in an array in the following way:

class ViewController: UIViewController, UITableViewDataSource, 
{

            testBook.setBookTitle(title: "Harry Potter")
            testBook.setbookPage(page: 12)

            myBookCollection.append(testBook)

//            testBook = Book()

            testBook.setBookTitle(title: "testing")
            testBook.setbookPage(page: 66)

            myBookCollection.append(testBook)



            for books in myBookCollection
            {
                print(books.getBookTitle())
            }

}

If I run the above I my books in the array are stored as ["testing","testing"] as opposed to ["Harry Potter","testing"].

However If I uncomment testbook = Book() it works fine. Can someone please explain why this is the case ?

thanks,

Nant
  • 569
  • 2
  • 10
  • 24

2 Answers2

1

the array doesn’t hold a copy of your Book instance , it simply hold the pointer to the object(book). When you change the title and page you are not changing the array contents, you are changing the object which testbook is pointing to. however if you uncomment the line testBook = Book() . You create a new instance of Book class and you set the pointer testbook to the newly created object and all the changes from this point is done on the new object and that’s why it works as it should. And all of this is true if your are using a class in your array , if you use struct that’s another story

  • is there a way where I can hold a copy of the Book instance instead of just pointing at it when appending ? – Nant Mar 02 '17 at 22:24
  • 1
    because you have a pointer in your array the arc memory management system keeps the actual object alive as long as it has at least one pointer to it (if it is in the array it counts as one) so there is no need for copy , but you can always copy an object by subclassing NSObject and conforming to NSCopying protocol –  Mar 02 '17 at 22:28
1

Reference vs. value type.

If you define Book as a class, when you add it to array, it does not copy our object. testBook and myBookCollection[0] are holding the same object. When you change either one, the other will be affected. If you add testbook = Book(), then testBook is now pointing to a different object in memory and what you do it it has no impact on myBookCollection[0].

You can change Book from a class (reference type) to a struct (value type). In that case, when you add it to an array, it will add a copy to the array so what you do with testBook after doesn't affect it.

Here's a video from WWDC that demonstrates the exact problem you had: Building Better App with Value Types in Swift

Code Different
  • 90,614
  • 16
  • 144
  • 163
  • Just to understand: why would I ever use a class instead of a struct then ? – Nant Mar 02 '17 at 22:25
  • [This question](http://stackoverflow.com/questions/24232799/why-choose-struct-over-class/24232845) has a length discussion on the pros and cons of each – Code Different Mar 02 '17 at 22:33