Swift:if 语句中的预期声明错误

1 uiviewcontroller ios swift

我有一个TableViewController和一个ViewController。我试图将两者链接在一起并在ViewController依赖项中打印某个语句,具体取决于TableViewControler.

我声明了一个名为的公共变量,rowCounter并在TableViewController我设置的类中rowCounter等于indexPath.

然后,在TableViewController我尝试使用 if 语句访问rowCounter变量并打印依赖于数字的语句,但我不断收到“预期声明”错误。我究竟做错了什么?

这是代码:

TableViewController:

import UIKit

var rowCounter: Int = 0

class TableTableViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

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

override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {

    rowCounter = indexPath.row

    return indexPath
}
Run Code Online (Sandbox Code Playgroud)

ViewController:

import UIKit

class ViewController: UIViewController {

if rowCounter == 1 {

println("test")

}

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

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

}
Run Code Online (Sandbox Code Playgroud)

Chi*_*buZ 5

if rowCounter == 1 {

println("test")

}
Run Code Online (Sandbox Code Playgroud)

在 func 之外