在目标上调用 xxx 时抛出了发送到实例的无法识别的选择器 [英] Unrecognized selector sent to instance was thrown while invoking xxx on target

查看:16
本文介绍了在目标上调用 xxx 时抛出了发送到实例的无法识别的选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 AppDelegate.m 中声明了一个新方法,例如:

I declared a new method in AppDelegate.m, like:

-(void):(UIApplication *)aMethod :(NSDictionary *)launchOptions{
......
    [UMessage registerForRemoteNotificationsWithLaunchOptions:launchOptions Entity:entity 
     completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
        }else{
        }
    }];
......
}

在我的 AppDelegate.h 中:

in my AppDelegate.h:

- (void)aMethod;

在我的 anotherClass.m 中:

in my anotherClass.m:

  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  [appDelegate aMethod];

当我在 anotherClass.m 中运行代码时,出现错误.有谁知道我错在哪里?

And when I run the code in anotherClass.m I got the error. Does anyone know where I am wrong?

推荐答案

发生错误是因为 .h 和 .m 中方法的签名不匹配,而对于外部类,.h 文件是相关的.

The error occurs because the signatures of the methods in .h and .m don't match and for external classes the .h file is relevant.

但是还有一个更严重的错误/误解.实际上你正在扩展 UIApplicationDelegate 这没有任何意义.将特定实例作为第一个参数传递的方法只有在 delegate 声明的实例中被调用时才有用,在你的例子中是 UIApplication.

But there is a more significant mistake/misunderstanding. Actually you are extending UIApplicationDelegate which makes no sense. Methods which pass a specific instance as first parameter are only useful when being called in the instance where the delegate is declared in, in your case in UIApplication.

AppDelegate 中声明但从任意类调用的方法的签名应与普通方法相同

The signature of a method declared in AppDelegate but called from an arbitrary class should be the same as a normal method

.h

-(void)aMethod:(NSDictionary *)launchOptions;

.m

-(void)aMethod:(NSDictionary *)launchOptions { 
    ...
    [UMessage registerForRemoteNotificationsWithLaunchOptions:launchOptions 
                                                       Entity:entity 
                                            completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
        }else{
        }
    }];
...
}

并使用它

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSDictionary  *options = ...
[appDelegate aMethod: options];

这篇关于在目标上调用 xxx 时抛出了发送到实例的无法识别的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆