One solution is to store all your variables in an array. The indexes for the variables that you store in this array will correspond to the indexes you want to include in the variable name.
Create an instance variable at the top of your view controller:
var people = [WhateverTypePersonIs]()
Then create a loop that will store as many people as you like in this instance variable:
for var i = 0; i < someVariable; i++ { let person =
If you ever need to get what "person_2" would be with how you tried to solve your problem, for example, you can access this person using people[2] .
source share