gpt4 book ai didi

swift - 学习 swift : expressions are not allowed at the top level

转载 作者:行者123 更新时间:2023-11-28 06:42:47 26 4
gpt4 key购买 nike

我正在学习 Swift。我遇到了无法解决的问题。

import UIKit

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

helloword("say")

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

expressions are not allowed at the top level

最佳答案

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

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

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

关于swift - 学习 swift : expressions are not allowed at the top level,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37474374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com