-2

looking for a possibility to update the view every day.

this is what I have tried... but it doesn't even print "test2"

var WordNumber = 0
override func viewDidLoad() {
    super.viewDidLoad()
    let userDefault = UserDefaults.standard
    if let lastRe = userDefault.dictionary(forKey: "lastRe") {
        print("test2")
        if let letztesDatum = lastRe ["date"] as? NSDate {
            print("test3")
            if let index = lastRe ["index"] as? Int {
                print("test4")
                if abs(letztesDatum.timeIntervalSinceNow) > 1 {
                    print("test5")
                    WordNumber = index + 1
                    let  _ : [NSObject : AnyObject] = ["date" as NSObject : NSDate(),
                                                       "index" as NSObject : WordNumber as AnyObject]
                }
                print("test6")
                nextDay() //update lbl
                userDefault.set(lastRe, forKey: "lastRe")
                userDefault.synchronize()
            }
        }
    }
}
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
rabest97
  • 5
  • 2
  • 1
    Remember, `UserDefaults` will have no values when an app is first installed. So if your code only adds a value inside code that checks if it has a value, then it will never have a value. – rmaddy May 16 '19 at 15:28
  • BTW - `synchronize()` is obsolete. Don't use it. – rmaddy May 16 '19 at 15:28
  • Just suggsetion bro that never user pyramid if let. this will lead you to more complexity. And yup about your print problem, i think @rmaddy is right(+1 vote to you bro) – singh.jitendra May 16 '19 at 17:53

2 Answers2

0

Simpler solution: Save only the day integer and add an observer to be notified when the day changes

override func viewDidLoad() {
    super.viewDidLoad()
    let today = Calendar.current.component(.day, from: Date())
    let lastRe =  UserDefaults.standard.integer(forKey: "lastRe")
    if today != lastRe {
        updateView(day: today)
    }
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    NotificationCenter.default.addObserver(forName: .NSCalendarDayChanged, object:nil, queue: .main) { [weak self] _ in 
       let today = Calendar.current.component(.day, from: Date())
       self?.updateView(day: today)
    }
}

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    NotificationCenter.default.removeObserver(self, name:.NSCalendarDayChanged, object:nil)
}

func updateView(day : Int) {
    nextDay()
    UserDefaults.standard.set(day, forKey: "lastRe")
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • thank you! that worked very well. is there an opportunity to show the same view on a different phone every day? This doesn't work with the UserDefault right? – rabest97 May 24 '19 at 08:00
  • Not on a different phone, for that purpose you need a sync service like CloudKit. – vadian May 24 '19 at 08:10
-1

if let lastRe = userDefault.dictionary(forKey: "lastRe")

The above will only execute the block if there is a value found. You are not setting a default value in UserDefaults for key lastRe – so the code is never executed.

Try setting a default value:

UserDefaults.Standard.set("my initial value here", forKey: "lastRe")

emma ray
  • 13,336
  • 1
  • 24
  • 50