Append problem in while loop

I am having a problem using the append function to add stuff to an array inside a while loop. The code is:
while sqlite3_step(stmt) == SQLITE_ROW
{
tmpRecord.Name = String(cString: sqlite3_column_text(stmt, 0))
tmpRecord.Phone = String(cString: sqlite3_column_text(stmt, 1))
tmpRecord.Street = String(cString: sqlite3_column_text(stmt, 2))
tmpRecord.City = String(cString: sqlite3_column_text(stmt, 3))
tmpRecord.State = String(cString: sqlite3_column_text(stmt, 4))

    theReturn.append(tmpRecord)
}

If I check the value of tmpRecord inside the loop it's fine, but when the loop ends theReturn just contains a bunch of copies of the same record.

It sounds like tmpRecord is an instance of a class. Is that right? If so, you'll need to create a new instance each iteration (just inside the while loop).

1 Like