【问题标题】:Swift error about consecutive declarations on a line关于一行连续声明的 Swift 错误
【发布时间】:2014-10-31 20:31:27
【问题描述】:

我不明白我的视图控制器中的这段代码有什么问题,最底线(带有单个 } 括号)不断出现两个错误,一个指出“一行上的连续声明必须用 ';' 分隔” ”和“预期声明”。当我在它引导我的地方添加分号时,它仍然说预期的声明错误....但是为了什么?谁能发现它有什么问题?

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

}

【问题讨论】:

    标签: ios xcode swift declaration


    【解决方案1】:

    此行之前缺少一个右大括号:

    class Counter {
    

    您的viewDidLoad() 方法没有正确关闭,所以会发生类和didReceiveMemoryWarning 被定义为viewDidLoad 的本地。

    适当的缩进通常会显示类似的错误...您是否正确缩进了代码?

    【讨论】:

      【解决方案2】:

      正如所写,class Counterfunc didReceiveMemoryWarning()viewDidLoad 内部。修复你的牙套。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多