学习swift:顶层不允许使用表达式

时间:2016-05-27 03:28:24

标签: swift

我正在学习斯威夫特。我遇到了一个无法解决的问题。

import UIKit

func helloword(str:String) {
    print(str)
}

helloword("say")

我使用helloword("say"),但Xcode告诉我错误:

  顶层不允许使用

表达式

1 个答案:

答案 0 :(得分:1)

您无法在文件中的任何位置简单地调用此方法。必须在控制流程中调用它。我的意思是在函数内部调用它。

例如,从viewDidLoad方法调用您的函数,如下所示:

override func viewDidLoad() {
self.helloword("say")  // here self is the View Controller itself
}