1

I want the user to be able to enter a new number and it will be added to what is currently saved in UserDefaults and then save that combined number in user defaults. Anyone have any idea how to do this? Thanks!

Code:

    let typeHoursInt = Double(typeHours.text!)!
    let typePayInt = Double(typePay.text!)!
    totalMade.text = String(typeHoursInt * typePayInt)

    UserDefaults.standard.set(totalMade.text, forKey: "savedMoney")
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Nicole L.
  • 23
  • 4

3 Answers3

3

Don't save strings to userDefaults. Save numbers.

Then:

  1. Read the value from defaults.

  2. Add your new value to the newly read value

  3. Save the new sum back to defaults.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • UserDefaults has the convenience methods `double(forKey:)`/`integer(forKey:)` (and corresponding set methods for both types) that let you seamlessly read and write Ints and doubles. – Duncan C Mar 08 '18 at 23:27
0

This does what you ask. You should store your totalMade variable in User Defaults as a Double, not a String. See below:

// your original code
let typeHoursInt = Double(typeHours.text!)!
let typePayInt = Double(typePay.text!)!

let total = typeHoursInt * typePayInt

totalMade.text = String(total)

// saving original value in User Defaults
UserDefaults.standard.set(total, forKey: "savedMoney")

// retrieving value from user defaults
var savedMoney = UserDefaults.standard.double(forKey: "savedMoney")

// adding to the retrieved value
savedMoney = savedMoney + 5.0

// resaving to User Defaults
UserDefaults.standard.set(savedMoney, forKey: "savedMoney")
podomunro
  • 495
  • 4
  • 16
  • Thank you for your response! I am getting a couple "Value of type 'Double' has no member 'text'" errors - any ideas how to fix this? – Nicole L. Mar 11 '18 at 15:48
  • @NicoleL. Ah, that’s because the name of your text field and the variable name for “totalMade” is the same. I’ll update the answer. – podomunro Mar 11 '18 at 15:50
  • That works! So, to get the value of what the user enters, I would use totalMade and convert it from a String to a Double and replace '5.0'? Again, thank you so much - I am still learning! – Nicole L. Mar 11 '18 at 16:21
  • @NicoleL. Yeah, replace it with the text field that the user uses to enter the value. If this answer has helped you, please mark it as the correct answer! – podomunro Mar 11 '18 at 16:23
  • Thank you so much for your help @podomunro ! Please let me know if there is anything else I can do to help you out with your score or anything! – Nicole L. Mar 11 '18 at 16:41
-1

Below you'll find "8" and "20", which are replacements for typeHours.text and typePay.text respectively.

  UserDefaults.standard.set(535, forKey: "savedMoney")

  if let typeHoursInt = Int("8"), let typePayInt = Int("20") {
    let totalMade = typeHoursInt * typePayInt
    let newTotal = UserDefaults.standard.integer(forKey: "savedMoney") + totalMade
    UserDefaults.standard.set(newTotal, forKey: "savedMoney")
  }
jnblanchard
  • 1,182
  • 12
  • 12
  • I didn't down vote but this code has a few issues. Never use `setValue(forKey:)` or `value(forKey:)` to set/get values in `UserDefaults`. Those are methods of key-value coding. Use the type-safe methods listed in the documentation for `UserDefaults`. – rmaddy Mar 08 '18 at 23:30
  • I don't see the never, seems like pretty standard kvo stuff. I changed it anyways. – jnblanchard Mar 09 '18 at 03:29
  • The point is that needlessly using KVC when it isn't needed costs you type safely and it adds some overhead. Only use it when you actually need it. – rmaddy Mar 09 '18 at 03:43
  • Is it needless to access my default as an optional? – jnblanchard Mar 09 '18 at 03:57
  • I have no idea what you mean by your last comment. – rmaddy Mar 09 '18 at 03:58
  • This question specifies swift 4. Value for key returns any kind of optional; is it needless to handle defaults as optionals? – jnblanchard Mar 09 '18 at 04:48
  • But you are not using `value(forKey:)`. `integer(forKey:)` doesn't return an optional. – rmaddy Mar 09 '18 at 04:57
  • No, I guess I don't understand your question. But we are getting way off track. If there is something you don't understand and you can't find an answer, post your own question if you wish. – rmaddy Mar 09 '18 at 05:02