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

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

问题描述

我正在尝试制作一款小游戏,现在我希望能够看到该游戏的高分。在第一个 UIViewController (viewcontroller1)中,您可以看到分数( UILabel 称为ourScore),并且该分数与( var score:String!)。分数已与



<$ p $的标签连接到另一个 UIViewController (viewcontroller2)上的标签lastest p> override func prepareForSegue(segue:(UIStoryboardSegue!),发送者:AnyObject!){
if(segue.identifier == segueTest){
var svc = segue! .destinationViewController为! PointsViewController;
}
}

得分将与另一个标签连接 UIViewController (2)查看最终分数。现在,我想也可以看到高分。当前,高分在代码中的第一个 UIViewController

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

,而另一个 UIViewController 是:

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

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

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

在上面的行中,它显示期望的声明



感谢您的帮助!

解决方案

要清除像我这样的菜鸟,当您尝试在ViewController中使用变量时会出现预期的声明错误 not



例如,此代码将为您提供预期的声明错误。

  class ViewController:UIViewController {

var savedText =
savedText = this //<-预期的声明错误

虽然此代码不会给您错误。

  class ViewController:UIViewController {

var savedText =

覆盖func viewDidLoad(){
super.viewDidLoad()
savedText = this //<-这里没有错误,因为您在函数
中使用了它


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;
       }
}

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

and at the other UIViewController it is:

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

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.

Thanks for the help!

解决方案

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天全站免登陆