Objective c 无法使选择器为我的Swift功能工作
我有一个混合快速和客观的C应用程序。swift应用程序使用一些Objective库来处理OAuth2身份验证。其中一部分是在OAuth2对令牌的请求完成后对委托方法的回调 以下代码正在使用我传入的选择器的Objective C库GTMOAuth2中执行:Objective c 无法使选择器为我的Swift功能工作,objective-c,oauth-2.0,swift,selector,gtm-oauth2,Objective C,Oauth 2.0,Swift,Selector,Gtm Oauth2,我有一个混合快速和客观的C应用程序。swift应用程序使用一些Objective库来处理OAuth2身份验证。其中一部分是在OAuth2对令牌的请求完成后对委托方法的回调 以下代码正在使用我传入的选择器的Objective C库GTMOAuth2中执行: if (delegate_ && finishedSelector_) { SEL sel = finishedSelector_; NSMethodSignature *sig = [delegate_ methodS
if (delegate_ && finishedSelector_) {
SEL sel = finishedSelector_;
NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setSelector:sel];
[invocation setTarget:delegate_];
[invocation setArgument:&self atIndex:2];
[invocation setArgument:&auth atIndex:3];
[invocation setArgument:&error atIndex:4];
[invocation invoke];
}
我希望调用的函数位于我的swift viewController中,如下所示:
func authentication(viewController: GTMOAuth2ViewControllerTouch, finishedWithAuth: GTMOAuth2Authentication, error: NSError)
{
if (error != nil)
{
var alertView: UIAlertView = UIAlertView(title: "Authorisation Failed", message: error.description, delegate: self, cancelButtonTitle: "Dismiss")
alertView.show()
}
else
{
// Authentication Succeeded
self.mytoken = finishedWithAuth.accessToken
}
}
我当前传递的选择器是:
let mySelector: Selector = Selector("authentication:viewController:finishedWithAuth:error:")
并被用作此调用中的参数:
let myViewController: GTMOAuth2ViewControllerTouch = GTMOAuth2ViewControllerTouch(authentication: auth, authorizationURL: authURL, keychainItemName: nil, delegate: self, finishedSelector: mySelector)
谁能告诉我为什么我的函数从未被调用?它似乎总是在创建NSInvocation的线路上失败
我尝试了多个选择器字符串,但每一个都失败了。我错过什么了吗
我还试着把@objc放在func名称前面,但没有成功。测试了它,它确实像Martin R说的那样
let mySelector: Selector = Selector("authentication:finishedWithAuth:error:")
当查看swift函数的选择器时,它的第一个参数通常是无名的。对它进行了测试,它确实像Martin R所说的那样
let mySelector: Selector = Selector("authentication:finishedWithAuth:error:")
当查看swift函数的选择器时,它的第一个参数通常是无名的。swift方法
func authentication(viewController: GTMOAuth2ViewControllerTouch,
finishedWithAuth: GTMOAuth2Authentication,
error: NSError)
暴露于Objective-C as中
-(void)authentication:(GTMOAuth2ViewControllerTouch *) viewController
finishedWithAuth:(GTMOAuth2Authentication *) finishedWithAuth
error:(NSError *)error
- (instancetype)initWithFoo:(NSInteger)foo bar:(NSInteger)bar
这意味着选择器是
Selector("authentication:finishedWithAuth:error:")
通常,第一个参数名称不是选择器的一部分。唯一的例外
是init方法,其中第一个参数名合并到Objective-C中
方法名。例如,Swift初始值设定项
init(foo: Int, bar: Int)
翻译成Objective-C作为
-(void)authentication:(GTMOAuth2ViewControllerTouch *) viewController
finishedWithAuth:(GTMOAuth2Authentication *) finishedWithAuth
error:(NSError *)error
- (instancetype)initWithFoo:(NSInteger)foo bar:(NSInteger)bar
选择器将是
Selector("initWithFoo:bar:")
斯威夫特方法
func authentication(viewController: GTMOAuth2ViewControllerTouch,
finishedWithAuth: GTMOAuth2Authentication,
error: NSError)
暴露于Objective-C as中
-(void)authentication:(GTMOAuth2ViewControllerTouch *) viewController
finishedWithAuth:(GTMOAuth2Authentication *) finishedWithAuth
error:(NSError *)error
- (instancetype)initWithFoo:(NSInteger)foo bar:(NSInteger)bar
这意味着选择器是
Selector("authentication:finishedWithAuth:error:")
通常,第一个参数名称不是选择器的一部分。唯一的例外
是init方法,其中第一个参数名合并到Objective-C中
方法名。例如,Swift初始值设定项
init(foo: Int, bar: Int)
翻译成Objective-C作为
-(void)authentication:(GTMOAuth2ViewControllerTouch *) viewController
finishedWithAuth:(GTMOAuth2Authentication *) finishedWithAuth
error:(NSError *)error
- (instancetype)initWithFoo:(NSInteger)foo bar:(NSInteger)bar
选择器将是
Selector("initWithFoo:bar:")
我假设它应该是Selectorauthentication:finishedWithAuth:error:但目前无法测试。@Martin的假设是正确的,我已经测试过了。选择器应该是Selectorauthentication:finishedWithAuth:error:我假设它应该是Selectorauthentication:finishedWithAuth:error:但目前无法测试。@Martin的假设是正确的,我已经测试过了。选择器应为Selectorauthentication:finishedWithAuth:error: