I'm having a problem adding items to a list inside the 'for' loop, where all items in the list are repeated with the last value entered.
Here's my NSManagedObject list
var listCursosNovos: [Cursos] = []
Here's my loop:
for i in 0..<JSON.count{
var resultadoWS = JSON[i] as [String:AnyObject]!
curso.codcurso = resultadoWS?["COD_CURSO"] as? String
//Here I see that 'curso' comes as I wanted it
print("Add curso: ",curso)
self.listCursosNovos.append(curso) //Here is the problem
}
Each 'course' item is shown to me on the console and everything works fine. The problem is that when I put this item in the list, it repeats the last value I entered. Example:
If I have 3 items within 'JSON' with the values:
{codcurso = "Zezinho"}
{codcurso = "Huginho"}
{codcurso = "Luizinho"}
After getting out of the loop and checking the result of my list
print("The final list is:",self.listCursosNovos)
They look like this:
{codcurso = "Luizinho"}
{codcurso = "Luizinho"}
{codcurso = "Luizinho"}
This is repeated regardless of how many items I have inside 'JSON' and the value repeated is always the last, I can not understand why
I hope someone can help me. Thank you!