-2

I created a string value and stored it in a UserDefault on one view controller like so:

letSwimmerOneName = "John"

UserDefaults.standard.set(String(swimmerOneName), forKey: "twoFreelayNameOne

And I'm trying to retrieve that data on a different view controller like so:

var swimmerOneName = UserDefaults.standard.string(forKey: "twoFreelayNameOne")

I'm not getting any errors or crashes, it just simply doesn't work. So I was wondering if UserDefaults transfer across the entire project or stay put in the view controller they are created in.

Leo Marcinkus
  • 78
  • 1
  • 7
  • 1
    It does work across the project. What makes you say "it just simply doesn't work"? – George Feb 16 '20 at 23:45
  • I have a variable to store the string that is typed into a text field. I then store that variable inside of the UserDefault all on one view controller. On the second view controller, I retrieve that UserDefault value and try and set that to the text of a label. Unfortunately, the label does not display that string that I tried storing in the UserDefault. Is it most likely the issue with the way I'm trying to display the string on the label? – Leo Marcinkus Feb 16 '20 at 23:51
  • 1
    Well you can test that. Before the setting into `UserDefaults`, `print(String(swimmerOneName))` and after retrieving use `print(swimmerOneName)`. What are the results? – George Feb 16 '20 at 23:54
  • I have tried and nothing is printing to console. I even tried: print("Is this working?"). And that didn't show up to console either... – Leo Marcinkus Feb 16 '20 at 23:59
  • I also just tried adding a breakpoint, and that hasn't been triggered where I put the print statement. If I add it to: ```override func viewDidLoad() {``` this should trigger it, correct? – Leo Marcinkus Feb 17 '20 at 00:04
  • This sounds like a completely different problem you are asking compared to the original question. And adding it to `viewDidLoad()` should trigger it if you are actually showing the view controller. – George Feb 17 '20 at 00:06
  • It's a little unrelated, I was just trying to test it as you said. I am able to trigger the first ```print(String(swimmerOneName))``` but the second print statement in the ```viewDidLoad()``` does not get triggered when a button is pressed to move to the next view. – Leo Marcinkus Feb 17 '20 at 00:09
  • 1
    Ask that as another question then. Do **not** just edit this question. – George Feb 17 '20 at 00:14
  • Try using `loadView()` instead then. If that doesn't work, I'm not sure so post a full question. – George Feb 17 '20 at 00:17
  • Weird, that doesn't work either. I will update the post once I have this issue sorted out first. Thanks for the help! I believe it is just a simple issue with the viewDidLoad() for some reason. – Leo Marcinkus Feb 17 '20 at 00:20
  • Needless to say, while you can share model data between view controllers using `UserDefaults`, this is an anti-pattern. I wouldn't recommend that technique. – Rob Feb 17 '20 at 06:15

1 Answers1

-3

You are able to retrieve data stored in UserDefaults across different view controllers. My issue was created on where I had placed the UserDefaults and in the way that I declared my variables that were to be assigned this UserDefault value. TLDR: Sloppy formatting.

Leo Marcinkus
  • 78
  • 1
  • 7