簡體   English   中英

Swift 3 /如何重用擴展

[英]Swift 3 / How to reuse extensions

我正在嘗試保存代碼並進行重構。

在我的項目中,我在多個UIViewController使用以下adMobBanner擴展。

整個擴展是可重用的,我只需要更改ViewController的名稱即可:

extension MyVC: GADInterstitialDelegate {

但是由於我在多個類中使用它,所以這些類的長度不必要地超過了。

有某種重用擴展名的方法嗎? 就像是:

func myExtension(vc: UIViewController) {
    extension vc: GADInterstitialDelegate {
    ....
    }
}

myExtension(MyViewController)調用

我知道這段代碼是胡說八道,但是它提供了我想轉置的想法。 有沒有類似的東西? 還是用重復代碼保存擴展代碼行的另一種選擇是什么? 非常感謝您的幫助。

我的擴展名:

extension MyViewController: GADInterstitialDelegate {

    // MARK: - Setup Ads
    func setupAds() {
        // Setup our interstitial ad initially
        interstitial.delegate = self
        interstitial.load(GADRequest())


    }

    // MARK: - Load Interstitial Ad
    func loadFullScreenAd() {
        // GADInterstitial's are single use. You have to create a new GADInterstitial for each presentation
        // So, if you'd like to show more than one GADInterstitial in your apps session we need this
        // This func will be used to create a new GADInterstitial after one has been displayed and dismissed
        interstitial = GADInterstitial(adUnitID: getAdmobInterstitial())


        interstitial.delegate = self
        interstitial.load(GADRequest())

    }


    // MARK: - Show Interstitial Ad
    func showFullScreenAd() {
        // Call this function when you want to present the interstitial ad
        // ie. game over, transition to another vc, etc...
        // Make sure you give atleast a few seconds for this ad to load before atempting to present it
        // For example, don't try to present this ad in viewDidAppear

        // Check if the interstitial ad is loaded before trying to present it

        if self.interstitial.isReady {

            self.interstitial.present(fromRootViewController: self)
        }
    }


    // MARK: - GADInterstitial Delegate Methods
    func interstitialDidReceiveAd(_ ad: GADInterstitial!) {
        print("interstitialDidReceiveAd")
        showFullScreenAd()
    }

    func interstitialWillPresentScreen(_ ad: GADInterstitial!) {
        print("interstitialWillPresentScreen")
        // If you needed to pause anything in your app this would be the place to do it
        // ie. sounds, game state, etc...
    }

    func interstitialDidDismissScreen(_ ad: GADInterstitial!) {
        print("interstitialDidDismissScreen")
        // The GADInterstitial has been shown and dismissed by the user
        // Lets load another one for the next time we want to show a GADInterstitial

        //loadFullScreenAd()

        // If you paused anything in the interstitialWillPresentScreen delegate method this is where you would resume it
    }

    func interstitial(_ ad: GADInterstitial!, didFailToReceiveAdWithError error: GADRequestError!) {
        print("interstitial didFailToReceiveAdWithError: \(error)")
    }
}

到目前為止,對我有用的是創建一個名為"Name regarding to the content about-extension of class的文件,然后將其放入名為Extensions的組文件夾中。因此,在您的情況下,我將這樣稱呼該文件:

GADInterstitialDelegate-UIViewControllerExtension.swift

里面的代碼是這樣的:

extension UIViewController: GADInterstitialDelegate {
// your reusable code
}

這只是我的方法,我認為也有其他好的方法

擴展所有受影響的視圖控制器的公共超類(可能是UIViewController本身),然后可以在所有子類中使用代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM