MazzaMan
MazzaMan

Reputation: 123

NSUser defaults not saving correctly?

So recently I have been making a practice app that has a textfield and a label. When you enter something into the textfield and hit save it should change the label to that text and save it. The reason why it saves is so when you open the app the text of the label is equal to the last thing you saved. The text will save again if you hit save on a new string in the text box.

The problem I am having is that the label won't appear with text until you save something new and the string isn't saving so their is nothing there even if you saved something.

Here is my code from the ViewController.swift:

    //
//  ViewController.swift
//  Help
//
//  Created by Lucas on 9/30/15.
//  Copyright (c) 2015 Lucas. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet var Savedlbl: UILabel!
    @IBOutlet var Textfield: UITextField!
    @IBOutlet var Label: UILabel!


    var current = ""
    var Saved = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        let currentDefault = NSUserDefaults.standardUserDefaults()

      Savedlbl.text = Saved



        if(currentDefault.valueForKey("Saved") != nil)
        {
            self.Saved = currentDefault.valueForKey("Saved") as! NSString! as String

        }
        // Do any additional setup after loading the view, typically from a nib.
    }
    @IBAction func Set(sender: AnyObject) {


        setall()


    }
func setall()
    {
        current = Textfield.text!
        Label.text = Textfield.text!



        let currentDefault = NSUserDefaults.standardUserDefaults()

       Saved = (currentDefault.valueForKey("saved") as? String)!
        currentDefault.setValue(Saved, forKey: "saved")


        Savedlbl.text = Textfield.text



         currentDefault.synchronize()
            }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

Here is where I think the problem is in the view did load that would make sense. When the app loads its not setting it to the last thing saved.

    import UIKit

class ViewController: UIViewController {

    @IBOutlet var Savedlbl: UILabel!
    @IBOutlet var Textfield: UITextField!
    @IBOutlet var Label: UILabel!


    var current = ""
    var Saved = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        let currentDefault = NSUserDefaults.standardUserDefaults()

      Savedlbl.text = Saved



        if(currentDefault.valueForKey("Saved") != nil)
        {
            self.Saved = currentDefault.valueForKey("Saved") as! NSString! as String

        }
        // Do any additional setup after loading the view, typically from a nib.
    }

Thanks and let me know if you need any more info, I will provide it as fast as I can!

Upvotes: 1

Views: 67

Answers (2)

Paulw11
Paulw11

Reputation: 114773

Your problems are that you are doing things in the wrong order. In viewDidLoad you are setting the textfield before you have retrieved the value from user defaults and in setall you are re-saving the previously saved value, not the new value.

Try:

import UIKit

class ViewController: UIViewController {

    var saved=""
    var current=""
    @IBOutlet var Savedlbl: UILabel!
    @IBOutlet var Textfield: UITextField!
    @IBOutlet var Label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        let currentDefault = NSUserDefaults.standardUserDefaults()
        self.saved=currentDefault.stringForKey("Saved") ?? ""
        self.Savedlbl.text=self.saved

    }

    @IBAction func Set(sender: AnyObject) {
        setall()
    }

    func setall() {
        self.current = self.Textfield.text!
        self.Label.text = self.current

        let currentDefault = NSUserDefaults.standardUserDefaults()
        currentDefault.setObject(self.current,forKey:"Saved")

        self.Savedlbl.text = self.Textfield.text
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Also, by convention variables should start with a lower case letter and classes with an upper case. I changed Saved to saved and Current to current but I didn't change the IBOutlets because you will need to remake the connection in IB for those, but you should.

Upvotes: 0

Duncan C
Duncan C

Reputation: 131398

Don't use valueForKey/setValue:forKey:. Those are KVC methods. You want setObject:forKey: and objectForKey:.

Upvotes: 1

Related Questions