0

background info: this application scans a color, then, if the user would like to, saves the color to a UITableView using UserDefaults. The variables in here are byproducts of the algorithm used earlier in the program.

When I look at the Table, instead of their being new elements, the one I saved is replaced.

Here is the code, I am trying to save this to userdefaults, but I don't think userdefaults is the problem.

these are the pre-defined arrays. they are not part of the class.

var NameList = [String]()
var HexCodeList = [String]()
var RedList = [CGFloat]()
var BlueList = [CGFloat]()
var GreenList = [CGFloat]()

this is the function that is adding the variables to their corresponding arrays and saving them to user defaults. this is part of a viewcontroller class

@IBAction func SaveColor(_ sender: Any) {

    let alertController = UIAlertController(title: "Save Color", message: "Please name the color you would like to save:", preferredStyle: .alert)

    let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in
        if let field = alertController.textFields?[0] {
            // store your data
            //ColorList.append(Color(name: field.text!, hexCode: self.hexCode, Red: self.Red, Green: self.Green, Blue: self.Blue))
            //UserDefaults.standard.set(ColorList, forKey: "ColorList")



            NameList.append(field.text!)
            UserDefaults.standard.set(NameList, forKey: "NameList")

            HexCodeList.append(self.hexCode)
            UserDefaults.standard.set(HexCodeList, forKey: "HexCodeList")

            RedList.append(self.Red)
            UserDefaults.standard.set(RedList, forKey: "RedList")

            GreenList.append(self.Green)
            UserDefaults.standard.set(GreenList, forKey: "GreenList")

            BlueList.append(self.Blue)
            UserDefaults.standard.set(BlueList, forKey: "BlueList")

            UserDefaults.standard.synchronize()

            print("NameList - 2nd", NameList)

        } else {
            // user did not fill field
        }
    }

I'm pretty sure this is all the code that matters, but if you need more to help me out I will gladly post it all

thanks much

1
  • Have you tried the proposed answer? Commented Feb 26, 2017 at 23:53

1 Answer 1

1

The problem is that you are creating new lists each time in the definition part

 var NameList = [String]() // Nothing here!

than adding something to [] array using .append, and than saving [something] to UserDefaults. This overwrites whatever progress saved in defaults with [something].

To save your progress you need to initialize your storage properties correctly

lazy var NameList: [String] = UserDefaults.standard.array(forKey: "NameList") as? [String] ?? [String]()

This sample code retrieve NameList from UserDefaults or initialize it as empty string array if nothing is saved to defaults. Use this snippet with all the lists. Hope this helps.

P.S. according to common naming conventions it's better to name your variables in lowercase like nameList etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.