鍍金池/ 問答/C  iOS/ 【iOS】協(xié)議方法采用@optional,使用報(bào)錯(cuò)

【iOS】協(xié)議方法采用@optional,使用報(bào)錯(cuò)

協(xié)議內(nèi)部有多個(gè)方法,全部使用@optional修飾,執(zhí)行其中一個(gè)方法,直接報(bào)錯(cuò),結(jié)果顯示reason: '-[BSGMatchViewController doSendMessageFromJPush:]: unrecognized selector sent to instance 0x1063a1800'
但是協(xié)議方法:doSendMessageFromJPush在其他頁(yè)面執(zhí)行,并沒有在BSGMatchViewController使用,為什么會(huì)報(bào)錯(cuò)并顯示以上報(bào)錯(cuò)信息呢?求解

協(xié)議是在AppDelegate頁(yè)面設(shè)置的,用于監(jiān)聽極光推送并通過協(xié)議發(fā)送對(duì)應(yīng)方法。

AppDelegate.h:

//極光推送收到消息后通過協(xié)議傳遞信息
@protocol BSGJPushRegistrationIDDelegate <NSObject>

@optional
//前兩個(gè)發(fā)送給首頁(yè)`viewController`,第三個(gè)根據(jù)極光推送發(fā)送給其他頁(yè)面
//發(fā)送極光推送registrationID
- (void)doSendRegistrationID:(NSString *)registrationID;
//發(fā)送極光推送消息
- (void)doSendMessageFromJPush:(NSMutableDictionary *)content;

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

@end

AppDelegate.m:
-(void)doSendMatchInfoFromJPush:(NSMutableDictionary *)content協(xié)議方法通過doJPushMatchWithUserInfo方法在- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler處調(diào)用:


- (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); // 需要執(zhí)行這個(gè)方法,選擇是否提醒用戶,有Badge、Sound、Alert三種類型可以選擇設(shè)置
    
    //此處使用極光推送,參數(shù)由后臺(tái)確認(rèn)并使用。
    [self doJPushMatchWithUserInfo:userInfo];
    
    
    
}

-(void)doJPushMatchWithUserInfo:(NSDictionary *)userInfo內(nèi)部調(diào)用協(xié)議方法:


-(void)doJPushMatchWithUserInfo:(NSDictionary *)userInfo{
    
    BSGLog(@"極光推送消息(全部打?。?@",userInfo);
    
    NSMutableDictionary * infoDictionary = [userInfo mutableCopy];
    
    //此處使用極光推送,參數(shù)由后臺(tái)確認(rèn)并使用。
    if (userInfo[@"webview_param"]  && userInfo[@"webview_param"][@"type"]) {
        
        [self.delegate doSendMatchInfoFromJPush:infoDictionary];
        
    }
    
}

有時(shí)候執(zhí)行到[self.delegate doSendMatchInfoFromJPush:infoDictionary];這一步會(huì)報(bào)錯(cuò),設(shè)了斷點(diǎn)以后,點(diǎn)擊兩次continue后才會(huì)報(bào)錯(cuò),報(bào)錯(cuò)信息如上。

回答
編輯回答
久舊酒

具體是在AppDelegate哪個(gè)回調(diào)設(shè)置的呢?調(diào)用的時(shí)機(jī)又是哪里呢?請(qǐng)?zhí)峁┚唧w代碼展示

2017年2月9日 07:36
編輯回答
離魂曲

謝謝邀請(qǐng)。
1.設(shè)置delegate設(shè)置錯(cuò)了對(duì)象
2.AppDelegate里面沒有實(shí)現(xiàn)這個(gè)方法
3.方法沒有添加@objc

2018年2月10日 21:09