从子类更改为基类会使协议实现无效吗?

时间:2018-10-16 07:47:15

标签: objective-c swift protocols optional

我想写一些实现协议的可重用代码。当将其添加到ViewController的扩展中时,一切都很好,ViewController是我自己的UIViewController的子类,但是当我将扩展名更改为UIViewController时,出现了错误消息: 实例方法“ adViewDidReceiveAd”几乎与协议“ GADBannerViewDelegate”的可选要求“ adViewDidReceiveAd”匹配

针对已实现的两种方法(具有正确的函数名称),并且代码停止运行。

XCode 8(Xcode 8 Warning "Instance method nearly matches optional requirement")中有类似的东西,但是他们说它已经修复。我正在使用版本10.0(10A255)。

这是给出该错误消息的代码: 扩展UIViewController:GADBannerViewDelegate {

//来自GADBannerViewDelegate(完全可选的协议)

@objc func adViewDidReceiveAd(_ bannerView: GADBannerView)  
{  
    bannerView.isHidden = false  
}  

@objc func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError)  
{  
    bannerView.isHidden = true  
    print("AdMob error:", error.localizedDescription)  
}  

只需将UIViewController更改为ViewController即可。

我想不出任何理由不能按原样工作。任何人都可以确定这里发生的事情吗?

在此处使用的协议是在Objective-C中定义的,并且方法是可选的,这可能是相关的。我添加了@objc作为尝试使其工作,但这没什么区别。

协议声明是这样的:

//
//  GADBannerViewDelegate.h
//  Google Mobile Ads SDK
//
//  Copyright 2011 Google Inc. All rights reserved.
//

#import <Foundation/Foundation.h>

#import <GoogleMobileAds/GADRequestError.h>
#import <GoogleMobileAds/GoogleMobileAdsDefines.h>

@class GADBannerView;

GAD_ASSUME_NONNULL_BEGIN

/// Delegate methods for receiving GADBannerView state change messages such as ad request status
/// and ad click lifecycle.
@protocol GADBannerViewDelegate<NSObject>

@optional

#pragma mark Ad Request Lifecycle Notifications

/// Tells the delegate that an ad request successfully received an ad. The delegate may want to add
/// the banner view to the view hierarchy if it hasn't been added yet.
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView;

/// Tells the delegate that an ad request failed. The failure is normally due to network
/// connectivity or ad availablility (i.e., no fill).
- (void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(GADRequestError *)error;

#pragma mark Click-Time Lifecycle Notifications

/// Tells the delegate that a full screen view will be presented in response to the user clicking on
/// an ad. The delegate may want to pause animations and time sensitive interactions.
- (void)adViewWillPresentScreen:(GADBannerView *)bannerView;

/// Tells the delegate that the full screen view will be dismissed.
- (void)adViewWillDismissScreen:(GADBannerView *)bannerView;

/// Tells the delegate that the full screen view has been dismissed. The delegate should restart
/// anything paused while handling adViewWillPresentScreen:.
- (void)adViewDidDismissScreen:(GADBannerView *)bannerView;

/// Tells the delegate that the user click will open another app, backgrounding the current
/// application. The standard UIApplicationDelegate methods, like applicationDidEnterBackground:,
/// are called immediately before this method is called.
- (void)adViewWillLeaveApplication:(GADBannerView *)bannerView;

@end

GAD_ASSUME_NONNULL_END

TIA 标记

0 个答案:

没有答案