获得高分的预期声明错误 [英] Expected declaration error for making high score

查看:26
本文介绍了获得高分的预期声明错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个小游戏,现在我想让它可以看到游戏的高分.在第一个 UIViewController (viewcontroller1) 中,您可以看到分数(UILabel 称为 ourScore)并且分数正在使用 (var score: String!).得分已经连接到另一个UIViewController(viewcontroller2)上的一个名为recentScore的标签,使用

I'm trying to make a little game, now I want to make it possible to see the high score of the game. At the first UIViewController (viewcontroller1) you can see the score (UILabel called ourScore) and the score is working with (var score: String!). The score has been connected to a label called recentScore at the other UIViewController (viewcontroller2) With the use of

override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!) {
        if (segue.identifier == "segueTest") {
            var svc = segue!.destinationViewController as! PointsViewController;
       }
}

分数将连接到另一个 UIViewController (2) 处的另一个标签以查看最终分数.现在我想让它也能看到高分.目前的高分是在代码中的第一个 UIViewController

the score will connect to the other label at the other UIViewController (2) to see the final score. Now I wanted to make it possible to see also the high score. Currently the high score is with in the code the following on the first UIViewController

let highscore = ourScore.text;
NSUserDefaults.standardUserDefaults().setObject(highscore, forKey: "Highscore")
NSUserDefaults.standardUserDefaults().synchronize()

在另一个 UIViewController 是:

highSchore.text = NSUserDefaults.standardUserDefaults().stringForKey("Highscore")

highScore 当前是第二个 UIViewController 中应该显示高分的标签,我在代码中遇到错误

highScore is currently the label in the second UIViewController where the high score should be showing, I got an error at the code

highSchore.text = NSUserDefaults.standardUserDefaults().stringForKey("Highscore")

在上面的行中,它表示预期声明.

In the above line, it says Expected declaration.

感谢您的帮助!

推荐答案

对于像我这样的菜鸟来说清楚,当您尝试在 ViewController 中使用变量时,您可能会得到预期的声明错误不是在一个函数中.

To be clear for rookies like me, you can get the expected declaration error when you try to use a variable in your ViewController not in a function.

例如这段代码会给你预期的声明错误.

For example this code will give you the expected declaration error.

class ViewController: UIViewController {

    var savedText = ""
    savedText = "this" //<- Expected declaration error

虽然这段代码不会给你错误.

While this code will not give you the error.

class ViewController: UIViewController {

    var savedText = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        savedText = "this" //<- No error here because you used it in a function

这篇关于获得高分的预期声明错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆