将对象附加到对象数组

时间:2021-12-14 00:38:16

I get an unexpected result when I append a new object onto an array of objects. The append seems to overwrite the existing object in the array then append itself.

当我将一个新对象附加到一个对象数组时,我得到一个意想不到的结果。 append似乎覆盖了数组中的现有对象,然后追加自己。

Any thoughts? Or am I missing something really simple.

有什么想法吗?或者我错过了一些非常简单的事情。

Here's test code from my playground:

这是我操场上的测试代码:

class myRecord {

    var firstName: String?
    var lastName: String?
     }

var myRecords = [myRecord]() 
var tempRecord = myRecord()


tempRecord.firstName = "John" 
tempRecord.lastName = "Brown" 
myRecords.append(tempRecord)

tempRecord.firstName = "Jane" 
tempRecord.lastName = "Doe" 
myRecords.append(tempRecord)

for x in 0..<myRecords.count {
    print(x, "=", myRecords[x].firstName!, myRecords[x].lastName!) }

Console output: 
0 = Jane Doe
1 = Jane Doe

4 个解决方案

#1


1  

This happens obviously because class are Reference Type. if your creating object of class that means your are creating memory reference of that class. And if you are changes property of that class that means your are changing the value of that reference in your memory heap. That why your last object (i.e Jane Doe) is replacing the value value of first Object(i.e John Brown).

这显然是因为类是引用类型。如果您创建类的对象意味着您正在创建该类的内存引用。如果您是该类的更改属性,则意味着您正在更改内存堆中该引用的值。这就是为什么你的最后一个对象(即Jane Doe)正在替换第一个Object的值(即John Brown)。

So now in Array both elements refers same memory reference that why you getting both same object(i.e Jon Doe). If you changes implementation to struct, you getting answer as per you are implementing, Because struct are Value Type. So it refers only value of object.

所以现在在Array中,两个元素都引用相同的内存引用,这就是为什么你得到两个相同的对象(即Jon Doe)。如果您将实现更改为struct,则根据您的实现获得答案,因为struct是Value Type。所以它只指对象的价值。

To be more specify About class and struct Please read this link

更多指定关于类和结构请阅读此链接

#2


0  

When you re-assigned the value of tempRecord.firstName and tempRecord.lastName, it overwrote the value at index 0 of the myRecords array to equal the new values you set.

当您重新分配tempRecord.firstName和tempRecord.lastName的值时,它会覆盖myRecords数组的索引0处的值,使其等于您设置的新值。

Index 0 and Index 1 of the myRecords array both point to the same tempRecord class instance in memory which holds the value

myRecords数组的索引0和索引1都指向内存中保存该值的相同tempRecord类实例

{ firstName: Jane, lastName: Doe }

{firstName:Jane,lastName:Doe}

In order to add multiple instances of the myRecord class, you could create a separate instance called tempRecords1 and append the values to the myRecords array like so:

为了添加myRecord类的多个实例,您可以创建一个名为tempRecords1的单独实例,并将值附加到myRecords数组,如下所示:

...

var tempRecord = myRecord()
var tempRecord1 = myRecord()

...

tempRecord1.firstName = "Jane"
tempRecord1.lastName = "Doe"
myRecords.append(tempRecord1)

#3


0  

Just replace 'class' with 'struct' as class are reference type and struct are value type.

只需将'class'替换为'struct',因为类是引用类型,struct是值类型。

struct myRecord {

    var firstName: String?
    var lastName: String?
}

var myRecords = [myRecord]()
var tempRecord = myRecord()


tempRecord.firstName = "John"
tempRecord.lastName = "Brown"
myRecords.append(tempRecord)

tempRecord.firstName = "Jane"
tempRecord.lastName = "Doe"
myRecords.append(tempRecord)

for x in 0..<myRecords.count {
    print(x, "=", myRecords[x].firstName!, myRecords[x].lastName!) }

#4


0  

just put after

刚放好

tempRecord.firstName = "John" 
tempRecord.lastName = "Brown" 
myRecords.append(tempRecord)

tempRecord = myRecords ()

then add jane doe.

然后添加珍妮母鹿。

you can use Struct instead of Class, depends on what you need.

你可以使用Struct而不是Class,取决于你需要什么。

#1


1  

This happens obviously because class are Reference Type. if your creating object of class that means your are creating memory reference of that class. And if you are changes property of that class that means your are changing the value of that reference in your memory heap. That why your last object (i.e Jane Doe) is replacing the value value of first Object(i.e John Brown).

这显然是因为类是引用类型。如果您创建类的对象意味着您正在创建该类的内存引用。如果您是该类的更改属性,则意味着您正在更改内存堆中该引用的值。这就是为什么你的最后一个对象(即Jane Doe)正在替换第一个Object的值(即John Brown)。

So now in Array both elements refers same memory reference that why you getting both same object(i.e Jon Doe). If you changes implementation to struct, you getting answer as per you are implementing, Because struct are Value Type. So it refers only value of object.

所以现在在Array中,两个元素都引用相同的内存引用,这就是为什么你得到两个相同的对象(即Jon Doe)。如果您将实现更改为struct,则根据您的实现获得答案,因为struct是Value Type。所以它只指对象的价值。

To be more specify About class and struct Please read this link

更多指定关于类和结构请阅读此链接

#2


0  

When you re-assigned the value of tempRecord.firstName and tempRecord.lastName, it overwrote the value at index 0 of the myRecords array to equal the new values you set.

当您重新分配tempRecord.firstName和tempRecord.lastName的值时,它会覆盖myRecords数组的索引0处的值,使其等于您设置的新值。

Index 0 and Index 1 of the myRecords array both point to the same tempRecord class instance in memory which holds the value

myRecords数组的索引0和索引1都指向内存中保存该值的相同tempRecord类实例

{ firstName: Jane, lastName: Doe }

{firstName:Jane,lastName:Doe}

In order to add multiple instances of the myRecord class, you could create a separate instance called tempRecords1 and append the values to the myRecords array like so:

为了添加myRecord类的多个实例,您可以创建一个名为tempRecords1的单独实例,并将值附加到myRecords数组,如下所示:

...

var tempRecord = myRecord()
var tempRecord1 = myRecord()

...

tempRecord1.firstName = "Jane"
tempRecord1.lastName = "Doe"
myRecords.append(tempRecord1)

#3


0  

Just replace 'class' with 'struct' as class are reference type and struct are value type.

只需将'class'替换为'struct',因为类是引用类型,struct是值类型。

struct myRecord {

    var firstName: String?
    var lastName: String?
}

var myRecords = [myRecord]()
var tempRecord = myRecord()


tempRecord.firstName = "John"
tempRecord.lastName = "Brown"
myRecords.append(tempRecord)

tempRecord.firstName = "Jane"
tempRecord.lastName = "Doe"
myRecords.append(tempRecord)

for x in 0..<myRecords.count {
    print(x, "=", myRecords[x].firstName!, myRecords[x].lastName!) }

#4


0  

just put after

刚放好

tempRecord.firstName = "John" 
tempRecord.lastName = "Brown" 
myRecords.append(tempRecord)

tempRecord = myRecords ()

then add jane doe.

然后添加珍妮母鹿。

you can use Struct instead of Class, depends on what you need.

你可以使用Struct而不是Class,取决于你需要什么。