2016-10-22 58 views
2

我試圖保存代碼和重構。Swift 3 /如何重用擴展

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

整體擴展可重複使用的,我只需要改變視圖控制器的名稱:

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)") 
    } 
} 

回答

1

延長所有受影響的視圖控制器(可能UIViewController本身)的共同父類,那麼您可以在所有子類使用的代碼。

+0

所以'擴展UIViewController:GADInterstitialDelegate {'? –

+0

是的,正好:-) – vadian

+0

非常感謝。我會接受你的答案,因爲它與第二個答案中的邏輯相同,你的第一個 –

2

到目前爲止,我的工作是創建一個名爲"Name regarding to the content about-extension of class的文件,然後將該文件放在名爲Extensions的組文件夾中。所以你的情況我會叫這樣的文件:

GADInterstitialDelegate-UIViewControllerExtension.swift 

而且裏面的代碼那樣:

extension UIViewController: GADInterstitialDelegate { 
// your reusable code 
} 

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

+2

非常感謝你的幫助和意見。但我會接受其他答案,因爲它是一樣的邏輯和公平:他是第一個(基於時間)。但upvote爲您的時間和精力 –

+0

歡迎,相信這是真的。我只是想放慢如何用最好的方式來描述它。最後,除了提示單獨的文件外,它完全一樣 – ronatory