【iOS】协议方法采用@optional,使用报错

协议内部有多个方法,全部使用@optional修饰,执行其中一个方法,直接报错,结果显示reason: '-[BSGMatchViewController doSendMessageFromJPush:]: unrecognized selector sent to instance 0x1063a1800'
但是协议方法:doSendMessageFromJPush在其他页面执行,并没有在BSGMatchViewController使用,为什么会报错并显示以上报错信息呢?求解

协议是在AppDelegate页面设置的,用于监听极光推送并通过协议发送对应方法。

AppDelegate.h:

//极光推送收到消息后通过协议传递信息
@protocol BSGJPushRegistrationIDDelegate <NSObject>

@optional
//前两个发送给首页`viewController`,第三个根据极光推送发送给其他页面
//发送极光推送registrationID
- (void)doSendRegistrationID:(NSString *)registrationID;
//发送极光推送消息
- (void)doSendMessageFromJPush:(NSMutableDictionary *)content;

-(void)doSendMatchInfoFromJPush:(NSMutableDictionary *)content;

@end

AppDelegate.m:
-(void)doSendMatchInfoFromJPush:(NSMutableDictionary *)content协议方法通过doJPushMatchWithUserInfo方法在- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler处调用:


- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    // Required
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
    
    //此处使用极光推送,参数由后台确认并使用。
    [self doJPushMatchWithUserInfo:userInfo];
    
    
    
}

-(void)doJPushMatchWithUserInfo:(NSDictionary *)userInfo内部调用协议方法:


-(void)doJPushMatchWithUserInfo:(NSDictionary *)userInfo{
    
    BSGLog(@"极光推送消息(全部打印):%@",userInfo);
    
    NSMutableDictionary * infoDictionary = [userInfo mutableCopy];
    
    //此处使用极光推送,参数由后台确认并使用。
    if (userInfo[@"webview_param"]  && userInfo[@"webview_param"][@"type"]) {
        
        [self.delegate doSendMatchInfoFromJPush:infoDictionary];
        
    }
    
}

有时候执行到[self.delegate doSendMatchInfoFromJPush:infoDictionary];这一步会报错,设了断点以后,点击两次continue后才会报错,报错信息如上。

阅读 4k
2 个回答

具体是在AppDelegate哪个回调设置的呢?调用的时机又是哪里呢?请提供具体代码展示

谢谢邀请。
1.设置delegate设置错了对象
2.AppDelegate里面没有实现这个方法
3.方法没有添加@objc

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题