(本文代码已升级至swift3)
1,给已有的类添加计算属性和计算静态属性
2,定义新的实例方法和类方法
3,提供新的构造器
4,定义下标脚本
5,是一个已有的类型符合某个协议
(注意:扩展只能添加新的计算型属性,不能添加存储型属性,也不能添加新的属性监视器)
示例1:给字符串String类添加下标脚本,支持索引访问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | extension String { subscript(start: Int , length: Int ) -> String { get { let index1 = self .index( self .startIndex, offsetBy: start) let index2 = self .index(index1, offsetBy: length) let range = Range (uncheckedBounds: (lower: index1, upper: index2)) return self .substring(with: range) } 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]) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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()) |