I have made a counter and i have managed to save and load that data. The problem is that the "var counter = 0" so when i press the counter up (+1) instead of going to the next number, it goes back to 0 because the variable is set to 0. heres my code
class ReminderClass: NSViewController {
@IBOutlet var Count: NSTextField!
override func viewDidLoad() {
readStats()
super.viewDidLoad()
}
func readStats() -> (NSString?){
let location = NSString(string:"location to my file").stringByExpandingTildeInPath
let fileContent = try? NSString(contentsOfFile: location, encoding: NSUTF8StringEncoding)
print(fileContent)
Count.stringValue = (fileContent?.stringByExpandingTildeInPath)!
fileContent?.stringByReplacingOccurrencesOfString(Count.stringValue, withString: Count.stringValue)
return fileContent
}
var counter = 0 //thats the problem
var counterfind = ""
@IBAction func PlusOne(sender: AnyObject) {
counter = counter + 1
Count.stringValue = NSString(format: "%i", counter) as String
}
@IBAction func MinusOne(sender: AnyObject) {
counter = counter - 1
Count.stringValue = NSString(format: "%i", counter) as String
}
@IBAction func save(sender: AnyObject) {
print(Count.stringValue)
let data = NSString(string:Count.stringValue)
let destPath = "path to my file"
_ = NSFileManager.defaultManager()
do {
try data.writeToFile(destPath, atomically: true, encoding: NSUTF8StringEncoding)
} catch let error as NSError {
print("Error: \(error)")
}
print(counter)
self.view.window?.close()
}