0

I am having a problem returning an integer value from UserDefaults. I must be formatting this incorrectly as it gives me different values depending on how I'm formatting it, so if there is a proper way to format this please let me know.

int = 1
UserDefaults.standard.set(x, forKey:"SavedKey\(int)")
print(int) // will print "1"
print(String(format:"SavedKey%i", int)) // will print "SavedKey-1065006928"

Why is this happening, and is there a proper way to format the second example so it will return the proper value?

Lucas Azzopardi
  • 1,131
  • 2
  • 11
  • 21
  • 5
    Your post mentions `UserDefaults` but your code doesn’t. – jcaron Nov 03 '17 at 21:09
  • 2
    Where did you get %i from? %d is what you usually use for int – Lou Franco Nov 03 '17 at 21:16
  • 1
    `%i` means the same as `%d`, @LouFranco. – jscs Nov 03 '17 at 22:23
  • %i and %d are not the same. They really not the same. I have tried to explain the different in short below and provided a link to an SO post that does it better. It may look like they are doing the same thing for output and even then, they are fundamentally different. Yes, they will both get you the same result in the given example, they are not the same. – Khanal Nov 04 '17 at 17:00

2 Answers2

3

I executed your code in playground and everything was fine. If you want formatted string containing something you can make it like this (from your example):

let int = 1
"SavedKey\(int)"
Mikhail Maslo
  • 616
  • 6
  • 13
3

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

Khanal
  • 788
  • 6
  • 14
  • 1
    %i is the same as %d. – rmaddy Nov 03 '17 at 22:31
  • @rmaddy, they are absolutely not the same. I have expanded on my comment to include that. I would accept, tongue and cheek, that for printing out value, they both act the same way, but they are not at all the same. – Khanal Nov 04 '17 at 16:55
  • Read the actual spec; it's linked from the Apple doc you linked: http://pubs.opengroup.org/onlinepubs/009695399/functions/printf.html They're identical. `scanf()` isn't the topic here. – jscs Nov 05 '17 at 22:05