Swift 集成adMob:错误:线程1:致命错误:

Swift 集成adMob:错误:线程1:致命错误:,swift,admob,swift4,Swift,Admob,Swift4,我正在尝试将admob添加到我的应用程序中,但出现错误 线程1:致命错误:在展开可选值时意外发现nil 我在developer.google.com和stackoverflow.com上也有关注,但我似乎无法修复它 这是我的密码: 导入UIKit 进口谷歌手机 类ViewController:UIViewController、GADBannerViewDelegate{ //Place your instance variables here let allQuestions = Questio

我正在尝试将admob添加到我的应用程序中,但出现错误

线程1:致命错误:在展开可选值时意外发现nil

我在developer.google.com和stackoverflow.com上也有关注,但我似乎无法修复它

这是我的密码:

导入UIKit 进口谷歌手机

类ViewController:UIViewController、GADBannerViewDelegate{

//Place your instance variables here
let allQuestions = QuestionBank()
var pickedAnswer : Bool = false
var questionNumber : Int = 0
var score : Int = 0
var bannerView: GADBannerView!
var banner: GADBannerView!


@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var GoogleBannerView: GADBannerView!

@IBOutlet weak var progressLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    banner = GADBannerView(frame: self.view.frame)
    GADMobileAds.configure(withApplicationID: "ca-app-pub-3940256099942544/2934735716")
    bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"       ***//Crashes Here***
    bannerView.rootViewController = self
    bannerView.load(GADRequest())
    bannerView.delegate = self

    /// Tells the delegate an ad request loaded an ad.
    func adViewDidReceiveAd(_ bannerView: GADBannerView) {
        print("adViewDidReceiveAd")
    }

    /// Tells the delegate an ad request failed.
    func adView(_ bannerView: GADBannerView,
                didFailToReceiveAdWithError error: GADRequestError) {
        print("adView:didFailToReceiveAdWithError: \(error.localizedDescription)")
    }

    /// Tells the delegate that a full-screen view will be presented in response
    /// to the user clicking on an ad.
    func adViewWillPresentScreen(_ bannerView: GADBannerView) {
        print("adViewWillPresentScreen")
    }

    /// Tells the delegate that the full-screen view will be dismissed.
    func adViewWillDismissScreen(_ bannerView: GADBannerView) {
        print("adViewWillDismissScreen")
    }

    /// Tells the delegate that the full-screen view has been dismissed.
    func adViewDidDismissScreen(_ bannerView: GADBannerView) {
        print("adViewDidDismissScreen")
    }

    /// Tells the delegate that a user click will open another app (such as
    /// the App Store), backgrounding the current app.
    func adViewWillLeaveApplication(_ bannerView: GADBannerView) {
        print("adViewWillLeaveApplication")
    }

    bannerView = GADBannerView(adSize: kGADAdSizeBanner)

    addBannerViewToView(bannerView)

    nextQuestion()
}

func addBannerViewToView(_ bannerView: GADBannerView) {
    bannerView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(bannerView)
    view.addConstraints(
        [NSLayoutConstraint(item: bannerView,
                            attribute: .bottom,
                            relatedBy: .equal,
                            toItem: bottomLayoutGuide,
                            attribute: .top,
                            multiplier: 1,
                            constant: 0),
         NSLayoutConstraint(item: bannerView,
                            attribute: .centerX,
                            relatedBy: .equal,
                            toItem: view,
                            attribute: .centerX,
                            multiplier: 1,
                            constant: 0)
        ])
}

感谢所有能提供帮助的人!!

您有两个GadBanerView类型的对象(banner和bannerView),看起来您正在混合它们


您正在尝试设置bannerView的adUnitId,但从未实例化过此横幅。

我删除了该横幅,但它仍然显示错误。您可以使用新代码编辑您的问题,以便我查看您的操作吗?