0

I am trying to get my app to countinously check a UserDefault to see if it changed. If the default, when the viewDidLoad, is at 20 and a moment afterward decreases to 15 i want the ViewController to refresh and show the correct number. Here is my code for reading the default.

import UIKit

let Ironcnt = UserDefaults.standard
let Goldcnt = UserDefaults.standard
let Fiveth = UserDefaults.standard

let shipcnt = UserDefaults.standard
let empirecnt = UserDefaults.standard

var fiveth = Int()
var iron = Int()
var gold = Int()
var ships = Int()
var empires = Int()

class twenty: UIViewController {
    @IBOutlet weak var fiveth1: UILabel!
    @IBOutlet weak var iron1: UILabel!
    @IBOutlet weak var gold1: UILabel!
    @IBOutlet weak var ships1: UILabel!
    @IBOutlet weak var empire1: UILabel!

@IBOutlet weak var webViewBG: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    let htmlPath = Bundle.main.path(forResource: "WebViewContent", ofType: "html")
    let htmlURL = URL(fileURLWithPath: htmlPath!)
    let html = try? Data(contentsOf: htmlURL)

    self.webViewBG.load(html!, mimeType: "text/html", textEncodingName: "UTF-8", baseURL: htmlURL.deletingLastPathComponent())

    if (Fiveth.value(forKey: "Fiveth") != nil){
        fiveth = Fiveth.value(forKey: "Fiveth") as! NSInteger!
        fiveth1.text = NSString(format: "Fiveth: %i", fiveth) as String
    }
    if (Ironcnt.value(forKey: "Ironcnt") != nil){
        iron = Ironcnt.value(forKey: "Ironcnt") as! NSInteger!
        iron1.text = NSString(format: "Iron: %i", iron) as String
    }
    if (Goldcnt.value(forKey: "Goldcnt") != nil){
        gold = Goldcnt.value(forKey: "Goldcnt") as! NSInteger!
        gold1.text = NSString(format: "Gold: %i", gold) as String
    }
}
Oren Edrich
  • 674
  • 1
  • 7
  • 23
  • 1
    Not able to see your UserDefault. And could you please explain what issue you are facing. – Dheeraj D Dec 28 '16 at 05:08
  • In your code doesn't have any part have `NSUserDefault`? – Tj3n Dec 28 '16 at 05:09
  • @DheerajD i updated my code, when the vied did load commands are called, the code is only done one time. I have a label that displays a userdefault and if the default is increased or decreased while in the viewcontroller i want the label to show the correct integer – Oren Edrich Dec 28 '16 at 05:13
  • Documentation for UserDefaults says that there is a UserDefaults.didChangeNotification for changes made in the same process or you can use "KVO"; here's a good pointer: http://stackoverflow.com/questions/5714161/nsuserdefaults-and-kvo-issues – Baglan Dec 28 '16 at 05:16
  • Thats not what i was looking for, i don't need to be notified. I have a label that shows an integer. The integer is saved using a userdefault. When the viewcontroller is called it will display the number that was last saved at that specific time, and it won't check it again unless i change screens. Without make a refresh button i want to see how i would have the app show the correct value for the integer. – Oren Edrich Dec 28 '16 at 05:22

1 Answers1

1

Refactor all of these, you dont need to create so many reference :

let Ironcnt = UserDefaults.standard
let Goldcnt = UserDefaults.standard
let Fiveth = UserDefaults.standard

let shipcnt = UserDefaults.standard
let empirecnt = UserDefaults.standard

var fiveth = Int()
var iron = Int()
var gold = Int()
var ships = Int()
var empires = Int()

to sth like this:

var fiveth: Int {
    set {
        fiveth1.text = newValue
        UserDefaults.standard.set(newValue, forKey: "fiveth")
    }

    get {
        return UserDefaults.standard.integer(forKey: "fiveth")
    }
}

then it will update both the userdefault and the label when you change the int

Tj3n
  • 9,837
  • 2
  • 24
  • 35