swift 单例的实现方法及实例
更新时间:2017年07月19日 09:31:34 投稿:lqh
这篇文章主要介绍了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 开发的文章还有很多,欢迎大家搜索参考,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
cmake ios终端下执行提示错误 iOS version not found, tested: [5.0;5.1;6
这篇文章主要介绍了cmake ios终端下执行提示错误 iOS version not found, tested: [5.0;5.1;6.0;6.1;7.0;8.3]的解决方案的相关资料,需要的朋友可以参考下2016-10-10
最新评论