简体   繁体   English

Swift 3 /如何重用扩展

[英]Swift 3 / How to reuse extensions

I'm trying to save code and refactor. 我正在尝试保存代码并进行重构。

In my project I'm using the following adMobBanner extension in several UIViewController s. 在我的项目中,我在多个UIViewController使用以下adMobBanner扩展。

The whole extension is reusable, I just need to change the name of the ViewController: 整个扩展是可重用的,我只需要更改ViewController的名称即可:

extension MyVC: GADInterstitialDelegate {

But since I'm using it in several classes, the length of those classes exceeds unnecessarily. 但是由于我在多个类中使用它,所以这些类的长度不必要地超过了。

Is there some kind of way to reuse the extension? 有某种重用扩展名的方法吗? Something like: 就像是:

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

Called by myExtension(MyViewController) myExtension(MyViewController)调用

I know that this code is nonsense, but it provides the idea I would like to transpose. 我知道这段代码是胡说八道,但是它提供了我想转置的想法。 Is there anything like that? 有没有类似的东西? Or what would be another option to save lines of codes for extensions with repeated code? 还是用重复代码保存扩展代码行的另一种选择是什么? Help is very appreciated. 非常感谢您的帮助。

My Extension: 我的扩展名:

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

What worked for me so far was to create an own file named like "Name regarding to the content about-extension of class and then I put the file in a group folder named Extensions . So in your case I would call the file like that: 到目前为止,对我有用的是创建一个名为"Name regarding to the content about-extension of class的文件,然后将其放入名为Extensions的组文件夹中。因此,在您的情况下,我将这样称呼该文件:

GADInterstitialDelegate-UIViewControllerExtension.swift

And the code inside like that: 里面的代码是这样的:

extension UIViewController: GADInterstitialDelegate {
// your reusable code
}

It's just my approach and I think there are also other good ones 这只是我的方法,我认为也有其他好的方法

扩展所有受影响的视图控制器的公共超类(可能是UIViewController本身),然后可以在所有子类中使用代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM