Swift 的单例

第一步 创建 SingleTest static let shareInstance = SingleTest()

class Singleton {

static let sharedInstance = Singleton()

}

 第二步 使用 SingleTest.shareInstance.getString()

 第三步 验证是否唯一 let str = SingleTest.shareInstance

1.单例的具体代码:

ViewController :

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        self.view.backgroundColor = UIColor.white

        let str = SingleTest.shareInstance

        print(Unmanaged.passUnretained(str).toOpaque() ) // 打印内存地址 

        // 单例的使用

        print(SingleTest.shareInstance.getString())


        //全局的一些东西.

        // 登陆成功记住用户名

        username = "bdgt"

        print(username)


        // 打印枚举

        print(alldata.loginUrl.rawValue)


        makeBtn()

        // Do any additional setup after loading the view, typically from a nib.

    }

    func makeBtn() {

        let btn = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 40))

        btn.setTitle("btn", for: UIControlState.normal)

        btn.backgroundColor = UIColor.lightGray

        btn.addTarget(self, action: #selector(btnClick), for: UIControlEvents.touchUpInside)

        self.view.addSubview(btn)

    }


    @objc func btnClick(){

    print("ddd")

        let vc = SecondViewController.init()

        present(vc, animated: true, completion: nil)

    }


    func changeString(newString: String) { // 第六步: 遵守协议

        print(newString)

    }


    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}

SecondViewController:

import UIKit

class SecondViewController: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

        let str = SingleTest.shareInstance

        print(Unmanaged.passUnretained(str).toOpaque() )  // 打印内存地址



        self.view.backgroundColor = UIColor.lightGray

        makeBtn()

        // Do any additional setup after loading the view.

    }


    func makeBtn() {

        let btn = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 40))

        btn.setTitle("返回", for: UIControlState.normal)

        btn.backgroundColor = UIColor.lightGray

        btn.addTarget(self, action: #selector(btnClick), for: UIControlEvents.touchUpInside)

        self.view.addSubview(btn)

    }


    @objc func btnClick(){

        print("ddd")

        dismiss(animated: true) {

            print("1")

        }

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}

SingleTest:

import UIKit

var username = "" //全局的,可以存储一些必备的.

enum alldata : String{  // 枚举  可以存储 接口

    case loginUrl = "http://www.dansdasda/login"

    case right = "右边"

}

class SingleTest {

    static let shareInstance = SingleTest()


    func getString() -> String {

        // 例如  获取当前的时间, 按照一定的格式,在这里写好之后, 所有的地方就都可以调用这里的了.

        return "共有方法,此处获取数据"

    }

}

/*第一种,类常量 Class constant , 建议使用    支持赖加载 线程安全

class Singleton  {

static let sharedInstance = Singleton()

}

*/

/*第二种  嵌套结构体变量格式

class Singleton {

class var sharedInstance: Singleton {

struct Static {

static let instance: Singleton = Singleton()

}

return Static.instance

}

}

*/

/* 第三种最不建议,但是最想oc 的创建方式  dispatch_once

class Singleton {

class var sharedInstance: Singleton {

struct Static {

static var onceToken: dispatch_once_t = 0

static var instance: Singleton? = nil

}

dispatch_once(&Static.onceToken) {

Static.instance = Singleton()

}

return Static.instance!

}

}

*/

你可能感兴趣的:(Swift 的单例)