private func:调用参数#1时缺少参数

时间:2016-01-27 14:52:28

标签: swift macos cocoa

嗯,这是另一个'呼叫'问题中参数#1的缺失参数。 (似乎Apple可以更好地命名其错误:-p)

在我的班级中,我正在调用一个私有函数$_SESSION[*],在那一行上,我在编译时遇到libraryVisibility()错误。不明白为什么。

无论如何,这是代码:

Missing argument for parameter #1 in call

2 个答案:

答案 0 :(得分:1)

您无法在属性

的默认初始化程序中调用实例函数

您可以将libraryVisibility()功能设为class功能:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    var libraryState : Bool = AppDelegate.libraryVisibility()

     private class func libraryVisibility() -> Bool {
        let homeUrl = NSURL(string: NSHomeDirectory())
        let libraryUrl = NSURL(string: "Library", relativeToURL: homeUrl)

        return libraryUrl!.hidden
    }
}

或者您可以将libraryState属性设为lazy属性:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    lazy var libraryState : Bool = self.libraryVisibility()

    private func libraryVisibility() -> Bool {
        let homeUrl = NSURL(string: NSHomeDirectory())
        let libraryUrl = NSURL(string: "Library", relativeToURL: homeUrl)

        return libraryUrl!.hidden
    }
}

该属性将在您第一次使用时初始化。

Mike Buss has a nice usage guide on how to use lazy variables.

答案 1 :(得分:0)

好的,我已经解决了,但并不完全确定为什么会这样:

libraryState变量设为默认值并将其设置在applicationDidFinishLaunching内似乎可以解决问题:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    var libraryState: Bool = false

    func applicationDidFinishLaunching(aNotification: NSNotification) {

        libraryState = libraryVisibility()

    }
}