It entirely depends on what that loop is doing. It's hard to give a better answer without knowing what the rest of the code looks like.
In general, constants are better than variables because they are safer (i.e., they can't accidentally be changed and screw something up somewhere). But they can also be more efficient.
To quote Paul:
Having both these options might seem pointless, after all you could just create a variable then never change it – why does it need to be made a constant? Well, it turns out that many programmers are – shock! – less than perfect at programming, and we make mistakes.
One of the advantages of separating constants and variables is that Xcode will tell us if we've made a mistake. If we say, "make this date a constant, because I know it will never change" then 10 lines later try to change it, Xcode will refuse to build our app.
Constants are also important because they let Xcode make decisions about the way it builds your app. If it knows a value will never change, it is able to apply optimizations to make your code run faster.
I'm not exactly sure what you mean by "an honest variable". The code snippet you suggest should only be used if there is some need for a value that can be changed by whatever is going on within the loop. Even then, depending on what you are doing, you could possibly do it a different way and keep the value a constant.
For instance:
var randomInts1: [Int] = []
for _ in 1...10 {
randomInts1.append(Int.random(in: 100...200))
}
//same thing can be accomplished with a constant
let randomInts2 = (1...10).map { _ in Int.random(in: 100...200) }
But, really, it all depends on what you are doing and what you need the value for as to whether you declare it as a var
or a let
.