这篇文章主要介绍了swift 单例的实现方法及实例的相关资料,需要的朋友可以参考下
swift 单例的实现方法及实例
定义Game类,代码如下:
public class Game { public var score = 0 public static let dafaultGame = Game() private init(){ } public func addscore(){ score += 10 } }
1、将init方法设置成private
2、定义静态变量
3、赋值给静态变量调用init方法
调用
let game1 = Game.dafaultGame game1.addscore() game1.score let game2 = Game.dafaultGame game2.addscore() game2.score
以上就是关于IOS 单例的简单实例详解,本站关于IOS 开发的文章还有很多,欢迎大家搜索参考,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
您可能感兴趣的文章:
- 深入理解Swift中单例模式的替换及Swift 3.0单例模式的实现
- IOS Swift3 四种单例模式详解及实例
- 通过示例分析Swift单例模式
- Swift中定义单例的方法实例
- swift中正确安全声明一个单例的方法实例