Swift语言的类扩展是一个强大的工具,我们可以通过类扩展完成如下事情:
1,给已有的类添加计算属性和计算静态属性
2,定义新的实例方法和类方法
3,提供新的构造器
4,定义下标脚本
5,是一个已有的类型符合某个协议
注意:
扩展只能添加新的计算型属性,不能添加存储型属性,也不能添加新的属性监视器。
在 Swift 4 中,extension 里可以访问 private 的属性。(过去 Swift3 中是不行的)
示例1:给字符串String类添加下标脚本,支持索引访问
extension String
{
subscript(start:Int, length:Int) -> String
{
get{
let index1 = self.index(self.startIndex, offsetBy: start)
let index2 = self.index(index1, offsetBy: length)
return String(self[index1..<index2])
}
set{
let tmp = self
var s = ""
var e = ""
for (idx, item) in tmp.characters.enumerated() {
if(idx < start)
{
s += "\(item)"
}
if(idx >= start + length)
{
e += "\(item)"
}
}
self = s + newValue + e
}
}
subscript(index:Int) -> String
{
get{
return String(self[self.index(self.startIndex, offsetBy: index)])
}
set{
let tmp = self
self = ""
for (idx, item) in tmp.characters.enumerated() {
if idx == index {
self += "\(newValue)"
}else{
self += "\(item)"
}
}
}
}
}
var str = "hangge.com"
print(str[7,3])
print(str[7])
str[7,3] = "COM"
str[0] = "H"
print(str[0,10])
运行结果如下:
示例2:给Double增加mm,cm等方法,进行进制转换
extension Double
{
func mm()->String
{
return "\(self/1)mm"
}
func cm()-> String
{
return "\(self/10)cm"
}
func dm()->String{
return "\(self/100)dm"
}
func m()->String
{
return "\(self/1000)m"
}
func km()->String
{
return "\(self/(1000*1000))km"
}
}
let value = 2000000000.0
print(value.mm())
print(value.cm())
print(value.dm())
print(value.m())
print(value.km())
运行结果如下:
(本文代码已升级至Swift4)
原文出自:www.hangge.com 转载请保留原文链接:http://www.hangge.com/blog/cache/detail_525.html
- 特点:
- 扩展中可以定义静态存储属性
static let bgTipsKeyStatic = "BgTipsKey" // it is ok
class let bgTipsKeyClass = "BgTipsKey"//Class stored properties not supported in classes; did you mean 'static'?
let bgTipsKeyLet = "" // Extensions must not contain stored properties
var bgTipsKeyVar = "" //Extensions must not contain stored properties