简体   繁体   English

为什么尝试追加时对象的值在Array中会发生变化?

[英]Why does value of object change in Array when trying to append?

I have a custom class "Book" and I am trying to save two books in an array in the following way: 我有一个自定义类“ Book”,并且尝试通过以下方式将两本书保存在一个数组中:

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"]. 如果我执行上述操作,则我数组中的书籍将存储为[“ testing”,“ testing”],而不是[“ Harry Potter”,“ testing”]。

However If I uncomment testbook = Book() it works fine. 但是,如果我取消注释testbook = Book(),则可以正常工作。 Can someone please explain why this is the case ? 有人可以解释为什么会这样吗?

thanks, 谢谢,

the array doesn't hold a copy of your Book instance , it simply hold the pointer to the object(book). 该数组不保存您的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. 当您更改标题和页面时,您并没有更改数组内容,而是在更改testbook指向的对象。 however if you uncomment the line testBook = Book() . 但是,如果您取消注释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. 您创建一个Book类的新实例,并将指针testbook设置为新创建的对象,并且testbook的所有更改都在该新对象上完成,这就是为什么它应该工作的原因。 And all of this is true if your are using a class in your array , if you use struct that's another story 如果您在数组中使用类,而使用struct又是另一回事,那么所有这些都是正确的

Reference vs. value type. 引用与值类型。

If you define Book as a class, when you add it to array, it does not copy our object. 如果您将Book定义为类,则将其添加到数组时不会复制我们的对象。 testBook and myBookCollection[0] are holding the same object. testBookmyBookCollection[0]持有相同的对象。 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] . 如果添加testbook = Book() ,则testBook现在指向内存中的其他对象,您执行的操作对myBookCollection[0]没有影响。

You can change Book from a class (reference type) to a struct (value type). 您可以将Book从类(引用类型)更改为struct (值类型)。 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. 在这种情况下,当您将其添加到数组时,它将向该数组添加一个副本 ,因此您对testBook所做的操作不会影响它。

Here's a video from WWDC that demonstrates the exact problem you had: Building Better App with Value Types in Swift 这是WWDC的视频,演示了您遇到的确切问题: 在Swift中使用值类型构建更好的应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM