1

Every time I run the app, and then re-run it, it saves the same items into the NSUserDefaults even if it is already there.

I tried to fix that with contains code, but it hasn't worked.

What am I missing?

    for days in results! {
        let nD = DayClass()
        nD.dayOfTheWeek = days[“D”] as! String
        let defaults = NSUserDefaults.standardUserDefaults()
            if var existingArr = defaults.arrayForKey("D") as? [String] {
                if existingArr.contains(days["D"] as! String) == false {
                    existingArr.append(nd.dayOfTheWeek)
                }
            } else {
                defaults.setObject([nD.dayOfTheWeek], forKey: "D")
            }
        }
victorpulak
  • 131
  • 2
  • 8
  • Can you give some more information of what you are try to do with the array. – Cing Mar 14 '16 at 21:58
  • Victor, you shouldn't use defaults.synchronize(). "So yeah, long story short, do not use synchronize in iOS 8 and later." -http://www.codingexplorer.com/nsuserdefaults-a-swift-introduction/ Also you should try only defining defaults (let defaults =...) once, at the top of your project. – owlswipe Mar 14 '16 at 22:03
  • @JohnRamos ok thanks! I changed that, and it is still doing the same thing. But maybe you were more looking at formatting tips, etc. which thanks. – victorpulak Mar 14 '16 at 23:22
  • @Cing I'm pulling "days of the week" from an API for example. It happens to pull Monday, puts it in the array. Then when I run the app a second time, it pulls Monday again, and puts it in the array again; where what I would want is to see if Monday is already in the array to not put it in there again. Make sense at all? Thanks! – victorpulak Mar 14 '16 at 23:25
  • @JohnRamos I updated my formatting and code! – victorpulak Mar 14 '16 at 23:41

1 Answers1

0

Every time I run the app, and then re-run it, it saves the same items into the NSUserDefaults even if it is already there.

Yes, because that's exactly what your code does:

defaults.setObject(existingArr, forKey: "D")

But what is existingArr? It's the defaults you've just loaded before:

if var existingArr = NSUserDefaults.standardUserDefaults().arrayForKey("D") as? [String]

So what happens is that when you enter the .contains branch, you always do the same operation: you save the existing array.

Contrary to what your comment in the code states, you're not appending anything to any array in that block, you're saving the existing one.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • Thanks for the comment. I was thinking `if var existingArr = NSUserDefaults.standardUserDefaults().arrayForKey("D")` would set up an array of the items I have in defaults, then `if existingArr.contains(nD.dayOfTheWeek) == false {` would check to see if I already have that day of the week in there and if not then it would add it. So is what you are saying that `defaults.setObject(existingArr, forKey: "D")` is the code that is wrong, then I updated my code to `append` to the existing array if that is what you are saying? Hope it is clearer now. Thanks! – victorpulak Mar 14 '16 at 23:41