Swift函数调用列表参数类型错误

Swift Function call list incorrect parameter type

我定义了下面的列表 swift class,并尝试从 viewcontroller 调用 sfAuthenticateUser。但是 Xcode 智能感知列出了错误的参数类型,而不是我定义的类型。

错误:无法将类型 'String' 的值转换为预期的参数类型 'APISFAuthentication'

Xcode 版本 7.1 (7B91b)

//View Controller方法调用如下

@IBAction func ActionNext(sender: AnyObject) {
    let sss =  APISFAuthentication.sfAuthenticateUser(<#T##APISFAuthentication#>)
}

// Class定义如下

class APISFAuthentication {
    init(x: Float, y: Float) {            
    }

    func sfAuthenticateUser(userEmail: String) -> Bool {
        let manager = AFHTTPRequestOperationManager()
        let postData = ["grant_type":"password","client_id":APISessionInfo.SF_CLIENT_ID,"client_secret":APISessionInfo.SF_CLIENT_SECRET,"username":APISessionInfo.SF_GUEST_USER,"password":APISessionInfo.SF_GUEST_USER_PASSWORD]

        manager.POST(APISessionInfo.SF_APP_URL,
            parameters: postData,
            success: { (operation, responseObject) in
                print("JSON: " + responseObject.description)
            },
            failure: { (operation, error) in
                print("Error: " + error.localizedDescription)

        })
        return true;
    }
}

请参考截图

问题是您在没有实际实例的情况下尝试调用实例函数。

您要么必须创建一个实例并调用该实例的方法:

let instance = APISFAuthentication(...)
instance. sfAuthenticateUser(...)

或将函数定义为 class 函数:

class func sfAuthenticateUser(userEmail: String) -> Bool {
    ...
}

解释:

Xcode 为您提供了什么而让您感到困惑的是 class 提供了通过向其传递实例来获取对其某些实例函数的引用的能力:

class ABC {
    func bla() -> String {
        return ""
    }
}

let instance = ABC()
let k = ABC.bla(instance) // k is of type () -> String

k 现在 函数 bla。您现在可以通过 k() 等方式调用 k