0

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()

}
5
  • 1
    Add some print statements into your code. To debug, you can either do breakpoints, or you can just add simple print statements to check on values as your code runs and functions fire. I would make sure that method is even attached to the button and called. Commented May 10, 2016 at 23:08
  • Why are you setting the counter to zero if you're not wanting zero? Commented May 10, 2016 at 23:08
  • i want the counter to equal the value of the textfield "count". But i have tried a few ways and xcode says that the variable can't be used in this class Commented May 10, 2016 at 23:44
  • 1
    it shoudnt matter if its set to 0 in the body of the class. the function should increment it, there is no function that resets it to 0. what are you trying to do exactly? Commented May 11, 2016 at 0:04
  • yeah thats what i worked out. later ill send a gif showing my problem, thank you all for the help! :) Commented May 11, 2016 at 0:08

3 Answers 3

1

Thank you all for helping me with the small issue I was having with my first mac application. I managed to figure out the problem. This is now my code :

class ReminderClass: NSViewController {




@IBOutlet var Count: NSTextField!

  override func viewDidLoad() {
    readStats()
    super.viewDidLoad()

}



func readStats() -> (NSString?){
    let location = NSString(string:"path-to-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
}



    @IBAction func PlusOne(sender: AnyObject) {
    let counter : String = Count.stringValue
    var num : Int? = Int(counter)
    num = num! + 1
    Count.stringValue = NSString(format: "%i", num!) as String
    }

@IBAction func MinusOne(sender: AnyObject) {
    let counter : String = Count.stringValue
    var num : Int? = Int(counter)
    num = num! - 1
    Count.stringValue = NSString(format: "%i", num!) as String
    }


@IBAction func save(sender: AnyObject) {
    print(Count.stringValue)
    let data = NSString(string:Count.stringValue)
    let destPath = "path-to-file"
    _ = NSFileManager.defaultManager()
    do {
        try data.writeToFile(destPath, atomically: true, encoding: NSUTF8StringEncoding)
    } catch let error as NSError {
        print("Error: \(error)")
    }

    self.view.window?.close()

}

Thank you all again! :)

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

Comments

0

Try setting var counter = Int()

1 Comment

ok thanks i will try that later im not at my computer right now
0

Try using this var counter :Int! = 0

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.