A few things I want to mention here:
- Your example should not return what you have claimed it does.
- %i is not the recommended string formatter for integers. Here is a reference should you need it: String Format Specifiers - Apple
- While
"SavedKey\(int)"
works to get the intended result, formatter if used correctly is going to be more powerful.For example:
let int = 1
print(int) // will print "1"
print(String(format:"SavedKey%3d", int)) // will print "SavedKey 1\n"
What we did here is formatted the in to take at least three characters so different values would look like so:
- 1 -> "SavedKey 1\n"
- 11 -> "SavedKey 11\n"
- 111 -> "SavedKey111\n"
- 1111 -> "SavedKey1111\n"
Added:
For output %i and %d may seem to be the same because the value stored in your variable is the same whether you assigned it a decimal, octal, or hex value so:
However, as far as the meaning goes, it is not the same. This would come into play if you were reading the value in.
%i would accept dec, octal, hex values, whereas %d would only accept decimal values.
A better explanation is In this SO Post