我定义了下面的 swift 类,并尝试从视图控制器调用 sfAuthenticateUser。但是 Xcode 智能感知列出了除我定义的类型之外的错误参数类型。
错误:无法将类型“String”的值转换为预期参数类型“APISFAuthentication”
Xcode 版本 7.1 (7B91b)
//ViewController方法调用如下
@IBAction func ActionNext(sender: AnyObject) {
let sss = APISFAuthentication.sfAuthenticateUser(<#T##APISFAuthentication#>)
}
// 类定义如下
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;
}
}
Please refer to the screen shot
![enter image description here](https://i.stack.imgur.com/IhTJn.png)
问题是您尝试在手头没有实际实例的情况下调用实例函数。
您必须创建一个实例并调用该实例上的方法:
let instance = APISFAuthentication(...)
instance. sfAuthenticateUser(...)
或将函数定义为类函数:
class func sfAuthenticateUser(userEmail: String) -> Bool {
...
}
解释:
Xcode 为您提供的以及让您感到困惑的是,该类提供了通过向其传递实例来获取对其某些实例函数的引用的功能:
class ABC {
func bla() -> String {
return ""
}
}
let instance = ABC()
let k = ABC.bla(instance) // k is of type () -> String
k
now is功能bla
。您现在可以致电k
via k()
etc.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)