0

I want to write some reuable code that implements a protocol. It all went fine when it was added in an extension to ViewController, my own subclass of UIViewController, but when I changed the extension to UIViewController, I get the error message: Instance method 'adViewDidReceiveAd' nearly matches optional requirement 'adViewDidReceiveAd' of protocol 'GADBannerViewDelegate'

against both the methods implemented (with the correct function name), and the code stops getting run.

There was something like this back in XCode 8 (Xcode 8 Warning "Instance method nearly matches optional requirement" ) but they were saying it had been fixed. I am using version 10.0 (10A255).

Here is the code that gives that error message: extension UIViewController: GADBannerViewDelegate {

// From GADBannerViewDelegate (fully optional protocol)

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

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

Simply changing UIViewController to ViewController makes it work.

I can't think of any reason that this shouldn't work as is. Can anyone give some certainty on what is up here?

It may be relevant that the protocol used here is defined in Objective-C, and the methods are optional. I have added @objc as an attempt to get it to work, but that made no difference.

The protocol declaration is this:

//
//  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 Mark

MarkAurelius
  • 1,203
  • 1
  • 14
  • 27

1 Answers1

0

I had a similar problem, but just cleaning my build folder made it go away. :/

Tyler A.
  • 3,048
  • 1
  • 23
  • 27