关于一行上连续声明的Swift错误

时间:2014-10-31 20:31:27

标签: ios xcode swift declaration

我不明白我的View Controller中的代码有什么问题,最底线(带有单个}括号)不断出现两个错误,一个说“一行上的连续声明必须用';'分隔” “和”预期声明“。当我添加分号时,它指示我它仍然说预期的声明错误....但是为了什么?任何人都可以发现它有什么问题吗?

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    var testObject = PFObject(className:"TestObject")
    testObject["foo"] = "bar"
    testObject.saveInBackgroundWithTarget(nil, selector: nil)

    var voteCount = PFObject(className:"VoteCount")
    voteCount["votes"] = 0
    voteCount["optionName"] = "Crepes"
    voteCount.incrementKey("votes")
    voteCount.saveEventually()

    var query = PFQuery(className:"VoteCount")
    query.getObjectInBackgroundWithId("e8KhneiSfw") {
        (voteCount: PFObject!, error: NSError!) -> Void in
        if error != nil {
            NSLog("%@", error)
        } else {
            voteCount["votes"] = 1
            voteCount.incrementKey("votes")
            voteCount.saveEventually()
        }
    }
class Counter {
    var voteCount: Int = 0
    func incrementBy(amount: Int, numberOfTimes times: Int) { voteCount += amount * times
        }
}

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

}

Screenshot for error

2 个答案:

答案 0 :(得分:1)

在这一行之前缺少一个右括号:

class Counter {

您的viewDidLoad()方法未正确关闭,因此会发生的情况是,该类和didReceiveMemoryWarning被定义为viewDidLoad的本地方法。

正确的缩进通常会显示错误......您是否正确缩进代码?

答案 1 :(得分:0)

如上所述,class Counterfunc didReceiveMemoryWarning()位于viewDidLoad内。修复你的牙箍。