Playground: Array output all instances


(Kna) #1

hello I'm learning to code, I can not understand why when I give the last command: "print (Board)", the output shows me only the first modified object and not all 5 instances .. how should I do to display them all when I print the output? thank you

[code]

import UIKit

var tre = arc4random_uniform(100)

class Creatura{

var Liv: Int
var PV:(Int,Int)
var FM: Int
var Pos: Int

init(Liv:Int,PV:(Int,Int),FM:Int,Pos:Int){
    self.Liv = Liv
    self.PV = PV
    self.FM = FM
    self.Pos = Pos
}

convenience init(){
   self.init(Liv:1,PV:(10,50),FM:1,Pos:1)

}
func description() {
print("Liv: (Liv)","PV: (PV.0)/(PV.1)","FM: (FM)","Pos: (Pos)")
}
}
var eroe = Creatura()

func CreaPlayer(eroe:Creatura)->Creatura{
var uno = arc4random_uniform(15)
if (uno < 8){
uno = 8
}
var due = arc4random_uniform(100)
if (due < 50){
due = 50
}
let tre = arc4random_uniform(100)
eroe.FM = Int(uno)
eroe.PV = (Int(due),Int(due))
eroe.Pos = Int(tre)

return eroe

}
var NEroe = CreaPlayer(eroe: eroe)
NEroe.description()
var NEroe2 = CreaPlayer(eroe: eroe)
NEroe2.description()
var NEroe3 = CreaPlayer(eroe: eroe)
NEroe3.description()
var NEroe4 = CreaPlayer(eroe: eroe)
NEroe4.description()
var NEroe5 = CreaPlayer(eroe: eroe)
NEroe5.description()
var myArray = Creatura
myArray.append(NEroe)
myArray.append(NEroe2)
myArray.append(NEroe3)
myArray.append(NEroe4)
myArray.append(NEroe5)

let cerchio = "\u{25CB}"
let triangolo = "\u{25BC}"
var Board : [String] = Array(repeating: cerchio, count: 99)

Board[NEroe.Pos] = triangolo
Board[NEroe2.Pos] = triangolo
Board[NEroe3.Pos] = triangolo
Board[NEroe4.Pos] = triangolo
Board[NEroe5.Pos] = triangolo

var cip = Board[NEroe4.Pos]

print(Board)
[\code]

output:
Liv: 1 PV: 50/50 FM: 9 Pos: 51

Liv: 1 PV: 63/63 FM: 12 Pos: 27

Liv: 1 PV: 63/63 FM: 8 Pos: 0

Liv: 1 PV: 50/50 FM: 8 Pos: 94

Liv: 1 PV: 50/50 FM: 10 Pos: 80

[" ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "▼", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "]


(Steven Van Impe) #2

This is because your function CreaPlayer(eroe:Creatura)->Creatura isn't creating a Creatura, it's only modifiying the one you pass to it.

Creatura is a class, which means variables of that type hold a reference to it, so var eroe = Creatura() creates a Creatura and eroe points to it.

Then, var NEroe = CreaPlayer(eroe: eroe) passes eroe into CreaPlayer, which changes it, and returns it. NEroe then points to the same Creatura as eroe. CreaPlayer didn't actually create anything.

Next, var NEroe2 = CreaPlayer(eroe: eroe) passes eroe again to the function, which again returns it, so NEroe2 ends up pointing to the same instance as eroe and NEroe.

In the end, eroe and NEroe up to NEroe5 all point to the same Creatura, because you've only ever created one of it.

Your prints of the description seem to work because you always print the latest value. However, when you change eroe in CreaPlayer, you're also changing NEroe, NEroe2, ... because they all point to the same thing.

So

Board[NEroe.Pos] = triangolo
Board[NEroe2.Pos] = triangolo
Board[NEroe3.Pos] = triangolo
Board[NEroe4.Pos] = triangolo
Board[NEroe5.Pos] = triangolo

all set the same position (which is the last position that got generated for NEroe5) five times.

To fix this, you should either create a new Creatura in CreaPlayer, or make Creatura a struct, in which case NEroe to NEroe5 get a modified copy of eroe and don't point to the same thing.


(Steven Van Impe) #3

FYI: to paste code snippets, use either `some code` or

```
some code
```