Justin Rose
Justin Rose

Reputation: 1185

NSUserDefaults to Save int - Simple fix

The error I am getting is cannot invoke '>' with an argument list of type '(() -> (), Int)'

The code works if var score = 0 is inside the score function.

Where exactly should I store this function for this to work, I am planning to make a button and when it is clicked the myScore label text is changed to String(score)so it will display on the score. I'm new to NSUserDefaults so go easy :)

import UIKit

var score = 0

class ViewController: UIViewController {

@IBOutlet weak var myScore: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

func score() {
    NSUserDefaults.standardUserDefaults().integerForKey("highscore")

    //Check if score is higher than NSUserDefaults stored value and change NSUserDefaults stored value if it's true
    if score > NSUserDefaults.standardUserDefaults().integerForKey("highscore") {
        NSUserDefaults.standardUserDefaults().setInteger(score, forKey: "highscore")
        NSUserDefaults.standardUserDefaults().synchronize()
    }

    NSUserDefaults.standardUserDefaults().integerForKey("highscore")

}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

Upvotes: 0

Views: 223

Answers (2)

Midhun MP
Midhun MP

Reputation: 107121

Your function name and that variable name are similar.That's why you are getting this error. Change anyone of them.

Also why are you just calling:

NSUserDefaults.standardUserDefaults().integerForKey("highscore")

You are not assigning the value or returning it, then why are you calling it several times ?

Change your method like:

func highScore() -> Int
{
    var highScore = NSUserDefaults.standardUserDefaults().integerForKey("highscore")

    //Check if score is higher than NSUserDefaults stored value and change NSUserDefaults stored value if it's true
    if score > highScore
    {
        NSUserDefaults.standardUserDefaults().setInteger(score, forKey: "highscore")
        NSUserDefaults.standardUserDefaults().synchronize()
        highScore = score
    }
    return highScore;
}

Upvotes: 1

jrturton
jrturton

Reputation: 119242

Type errors in Swift can be a little misleading, but this one makes sense.

cannot invoke '>' with an argument list of type '(() -> (), Int)'

Means you can't call the > function (yes, it's a function) with a left hand side value of type () -> () and a right hand side value of type Int.

The left hand side is a function type that takes no parameters and returns nothing - and you've got a function called score that does just that.

The compiler is using the function instead of the global variable with the same name. You should change the name of the score() function - it's vague and unclear anyway.

You may also want to make the score variable a property rather than a global variable, but that's a separate issue and not directly relevant to this problem.

Upvotes: 2

Related Questions