Я хочу написать некоторый код для повторного использования, который реализует протокол.Все прошло нормально, когда его добавили в расширение ViewController, моего собственного подкласса UIViewController, но когда я изменил расширение на UIViewController, я получаю сообщение об ошибке: Метод экземпляра 'adViewDidReceiveAd' почти соответствует необязательному требованию 'adViewDidReceiveAd' протокола 'GADBannerViewDelegate '
против обоих реализованных методов (с правильным именем функции), и код перестает запускаться.
Произошло нечто подобное в XCode 8 ( Xcode 8 Предупреждение«Метод экземпляра почти соответствует необязательному требованию» ), но они говорили, что оно исправлено.Я использую версию 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 Mark