如何在Swift中创建单例

3
我将使用单例来展示广告,但是它并不能很好地工作。当我不使用单例,而仅使用ViewController时,它能够很好地工作(可以通过"vampLoadStart"和"vampDidReceive")。
我该如何解决这个问题?
模式1:当我使用单例时(无法加载和显示广告)
VAMPAdReward.swift
import Foundation
import UIKit
import VAMP

class VAMPAdReward: NSObject,VAMPDelegate{

    static let sharedInstance = VAMPAdReward()

    var adReward:VAMP!

    override init() {
        super.init()

    }

    func loadAdReward(parentViewController: UIViewController) {
        adReward = VAMP()
        adReward.setPlacementId("26812") //test ID
        adReward.delegate = self
        adReward.setRootViewController(self)

    }

    func showAdReward(){
        if adReward.isReady() {
            print("show ad")
            adReward.show()
        }else{
            print("couldn't show ad")
        }
    }

    func vampLoadStart(_ placementId: String!, adnwName: String!) {
        print("start loading")
    }

    func vampDidReceive(_ placementId: String!, adnwName: String!) {
        print("finished loading")
    }
}

视图控制器

import UIKit

class ViewController: UIViewController {

    var adReward: VAMPAdReward!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white

        VAMPAdReward.sharedInstance.loadAdReward(parentViewController: self)

    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {


    super.touchesBegan(touches, with: event)

        //when touch screen, show Ad
        VAMPAdReward.sharedInstance.showAdReward()

    }
}

模式2:不使用Singleton(可以加载和展示广告)

import UIKit
import VAMP

class ViewController: UIViewController, VAMPDelegate {

    var ad: VAMP!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white

        //load ad
        ad = VAMP()
        ad.setPlacementId("59755") //test ID
        ad.delegate = self
        ad.setRootViewController(self)
        ad.load()

    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)

        ad.show()

    }

    func vampLoadStart(_ placementId: String!, adnwName: String!) {
        print("start loading") //through
    }

    func vampDidReceive(_ placementId: String!, adnwName: String!) {
        print("finished loading") //through
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

2
在Pattern1中,VAMPAdReward类的loadAdReward实现为什么是“adReward.delegate = parentViewController as! VAMPDelegate”,因为您的ViewController显然不符合该协议,并且该委托的实现在VAMPAdReward中,所以它不应该是单例。 - Gokul G
1
很抱歉我犯了一个错误。就像你所说的,“adReward.delegate = self”是正确的。我已经更新了代码。 - K.K.D
3
单例方法中没有看到adReward.load()的调用。 - Shehata Gamal
1
adReward.delegate = self 是正确的,但 adReward.setRootViewController(self) 是错误的,应该改回原来的 adReward.setRootViewController(parentViewController)。正如 @Sh_Khan 提到的那样,.load() 从未被调用。 - Gokul G
1
谢谢你的回答。我修复了我的代码,它运行得很好!非常感谢你,对于这个愚蠢的问题,我很抱歉... - K.K.D
2个回答

3

按照以下步骤完成单例类的创建:

// MARK: - Singleton

final class Singleton {

    // Can't init is singleton
    private init() { }

    // MARK: Shared Instance

    static let shared = Singleton()

    // MARK: Local Variable

    var emptyStringArray : [String] = []

}

1
谢谢!这对我很有帮助。 - K.K.D

2

单例模式的正确实现方法

func loadAdReward(parentViewController: UIViewController) {
    adReward = VAMP()
    adReward.setPlacementId("26812") //test ID
    adReward.delegate = self
    adReward.setRootViewController(parentViewController)
    adReward.load()
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接