一、MySingle类
import Foundation class MySingle{ //定义单例的属性 var name:String? var age:Int?
var height:Double? //定义类方法 class func shareInstance()->MySingle{ struct qzSingle{ static var predicate:dispatch_once_t = 0 static var instance:MySingle? = nil } dispatch_once(&qzSingle.predicate,{ qzSingle.instance = MySingle() }) return qzSingle.instance! } }
二、AppDelegate中调用
//单例的使用 var single = MySingle.shareInstance() single.name = "秦志伟" single.age = 24 single.height = 176.00 println(single)
执行结果:has 3 children
本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5093199.html,如需转载请自行联系原作者