Swift函数调用列表不正确的参数类型 [英] Swift Function call list incorrect parameter type
问题描述
我定义了下面的列表swift类,并尝试从viewcontroller调用sfAuthenticateUser。但是Xcode intellisense列出了错误的参数类型,而不是我定义的类型。
I define the below list swift class, and try to call the sfAuthenticateUser from the viewcontroller. But the Xcode intellisense list the wrong parameter type other than the type i defined.
错误:无法将'String'类型的值转换为预期的参数类型'APISFAuthentication'
ERROR : Cannot convert value of type 'String' to expected argument type 'APISFAuthentication'
Xcode版本7.1(7B91b)
Xcode Version 7.1 (7B91b)
//查看控制器方法调用如下
//View Controller method call as below
@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;
}
}
请参考屏幕截图
< a href =https://i.stack.imgur.com/IhTJn.png =nofollow noreferrer>
推荐答案
问题是您尝试调用实例函数而没有实际的实例。
The problem is that you try to call an instance function without having an actual instance at hand.
您必须创建一个实例并在该实例上调用该方法:
You either have to create an instance and call the method on that instance:
let instance = APISFAuthentication(...)
instance. sfAuthenticateUser(...)
或将函数定义为类函数:
or define the function as a class function:
class func sfAuthenticateUser(userEmail: String) -> Bool {
...
}
说明:
Xcode为您提供了什么以及您感到困惑的是,该类提供了通过向其传递实例来获取对其某些实例函数的引用的功能:
What Xcode offers you and what confuses you is the class offers the capability to get a reference to some of its instance functions by passing an instance to it:
class ABC {
func bla() -> String {
return ""
}
}
let instance = ABC()
let k = ABC.bla(instance) // k is of type () -> String
k
现在是函数 bla
。您现在可以通过 k()
等来拨打 k
。
k
now is the function bla
. You can now call k
via k()
etc.
这篇关于Swift函数调用列表不正确的参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!