swift中单列的写法
来源:互联网 发布:用户体验运营 知乎 编辑:程序博客网 时间:2024/05/23 01:11
swift中单列的写法常见有两种:
1.这种方式较简洁,推荐使用
//// MyManegerOne.swift// swift_SingleInstance//// Created by MrZhaoCn on 16/12/5.// Copyright © 2016年 MrZhaoCn. All rights reserved.// 单列写法1,推荐这种,简单import UIKitclass MyManegerOne: NSObject { private static let shareInstance = MyManegerOne() class func shareManeger() ->MyManegerOne { return shareInstance }}
2.这种方式也可以,相比于第一种推荐前者
//// MyManegerTwo.swift// swift_SingleInstance//// Created by MrZhaoCn on 16/12/5.// Copyright © 2016年 MrZhaoCn. All rights reserved.// 单利写法2import UIKitclass MyManegerTwo: NSObject { class func sharedManager() ->MyManegerTwo { struct Static { static let instance : MyManegerTwo = MyManegerTwo() } return Static.instance }}
下面在控制器里面测试一下
//// ViewController.swift// swift_SingleInstance//// Created by MrZhaoCn on 16/12/5.// Copyright © 2016年 MrZhaoCn. All rights reserved.//import UIKitclass ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let myManeger = MyManegerOne.shareManeger() print(myManeger) let myManeger1 = MyManegerOne.shareManeger() print(myManeger1) let myManegerTwo = MyManegerTwo.sharedManager() print(myManegerTwo) let myManegerTwo1 = MyManegerTwo.sharedManager() print(myManegerTwo1) }}
打印结果:可以看出正确的实现了swift中的单列
0 0
- swift中单列的写法
- swift中如何构造单列
- Swift 中CGAffineTransform的写法
- js单列和继承的一些写法
- Swift----单列的正确编写方式
- 官方单列写法
- swift 中不同于OC中写法的部分代码
- 单列设计模式几种写法的比较
- Swift中的"单列"
- Swift中NSJSONSerialization类抛出异常的处理写法
- swift 中跳转WKWebView,链接含有汉字的写法
- swift中跳转WKWebView 链接不含汉字的写法
- Swift单例的写法
- swift 单例的写法
- swift 属性的几个写法
- Swift单例的写法
- OC 三种单列写法
- 两列布局中单列定宽单列自适应布局的5种思路
- OpenGL中的轨迹球问题
- Log4j
- ios字体与安卓字体设置
- Linux RPM 命令参数使用详解
- Digital Library
- swift中单列的写法
- iOS10新特性
- java利用ide导出jar包
- maven install跳过test
- Java8 新特性
- Eclipse 拖拽功能(Drag and Drop)
- 01 找最大剩余体积 洛谷1049 装箱问题
- android调用浏览器打开网页链接
- C#发送邮件