Swift 调用非参数化函数时需要参数

Swift 调用非参数化函数时需要参数,swift,parameters,Swift,Parameters,我用Duncan C的帖子写了这个函数。我没有输入参数,但在调用函数Xcode时需要一个ViewController参数。我怎样才能解决这个问题 编辑:我添加了其余的代码。什么在破坏assign()函数 电话: 您已经将assign()定义为ViewController()类的实例方法,这意味着必须在 该类的实例 如果尝试使用初始化该类的属性 let dictArray = assign() 然后,assign被视为该类型的“curried function” ViewController

我用Duncan C的帖子写了这个函数。我没有输入参数,但在调用函数Xcode时需要一个ViewController参数。我怎样才能解决这个问题

编辑:我添加了其余的代码。什么在破坏assign()函数

电话:


您已经将
assign()
定义为
ViewController()
类的实例方法,这意味着必须在 该类的实例

如果尝试使用初始化该类的属性

let dictArray = assign()
然后,
assign
被视为该类型的“curried function”

ViewController -> () -> [String]
这解释了Xcode中意外的自动完成(请参见 )

最简单的 解决方案是将
assign()
函数移出
ViewController
类并将其定义为“自由函数” (或者为该函数选择一个更好的名称):


您需要提供更多的上下文。如果我将此方法复制到游乐场,然后键入
let dictArray=assign()
,它工作得非常好(某种程度上……它肯定没有这个问题)。我编辑了问题并添加了其余的代码。您正在为iOS编写代码,并试图使用Mac上的文档文件夹路径?@LeonardoSavioDabus。。在模拟器上进行测试是合理的。@LeonardoSavioDabus正如您在assign()func中的注释中所看到的,我编写并注释了。分发时,我将使用它们。这是给模拟器的。
ViewController -> () -> [String]
import UIKit

func getWordList() -> [String] {
    let path = NSBundle.mainBundle().pathForResource("words", ofType: "txt")!
    let content = String(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: nil)
    let wordList = content!.componentsSeparatedByString("\n")
    return wordList
}

class ViewController: UIViewController {

    let wordList = getWordList()

    // ...
}