前端之家收集整理的这篇文章主要介绍了
Swift教程之扩展,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
import Foundation
//扩展就是添加新功能。扩展和 Objective-C 中的分类(categories)类似。【不过与Objective-C不同的是,Swift 的扩展没有名字。】
//扩展可以添加新的计算属性,但是不可以添加存储属性,也不可以向已有属性添加属性观测器(property observers)。
//MARK:----------计算型属性(Computed Properties)-----
//单位转换
extension Double
{
var km: Double { return self * 1_000.0 }
var m : Double { return self }
var cm: Double { return self / 100.0 }
var mm: Double { return self / 1_000.0 }
var ft: Double { return self / 3.28084 }
}
let oneInch = 25.4.mm
print("Oneinch is \(oneInch) meters")
// 打印输出:"One inch is 0.0254 meters"
let threeFeet = 3.ft
print("Three feet is \(threeFeet) meters")
// 打印输出:"Three feet is 0.914399970739201 meters"
let aMarathon = 42.km + 195.m
print("A marathon is \(aMarathon) meters long")
// 打印输出:"A marathon is 42195.0 meters long"
//MARK:----------构造器(Initializers)--------
struct Size
{
var width = 0.0,height = 0.0
}
struct Point
{
var x = 0.0,y = 0.0
}
struct Rect
{
var origin = Point()
var size = Size()
}
let defaultRect = Rect()
let memberwiseRect = Rect(origin: Point(x: 2.0,y: 2.0),size: Size(width: 5.0,height: 5.0))
extension Rect
{
init(center: Point,size: Size)
{
let originX = center.x - (size.width / 2)
let originY = center.y - (size.height / 2)
self.init(origin: Point(x: originX,y: originY),size: size)
}
}
let centerRect = Rect(center: Point(x: 4.0,y: 4.0),size: Size(width: 3.0,height: 3.0))
// centerRect的原点是 (2.5,2.5),大小是 (3.0,3.0)
//MARK:----------方法(Methods)---------
//向Int类型添加一个名为repetitions的新实例方法,方法使用了一个() -> ()类型的单参数(single argument),表明函数没有参数而且没有返回值。
extension Int
{
func repetitions(task: () -> ())
{
for _ in 0..<self
{
task()
}
}
}
3.repetitions({
print("Hello!")
})
// Hello!
// Hello!
// Hello!
//MARK:----------修改实例方法(Mutating Instance Methods)---------
//通过扩展添加的实例方法也可以修改该实例本身。结构体和枚举类型中修改self或其属性的方法必须将该实例方法标注为mutating,正如来自原始实现的修改方法一样。
extension Int
{
mutating func square()
{
self = self * self
}
}
var someInt = 3
someInt.square()
// someInt 现在值是 9
//MARK:---------嵌套类型(Nested Types)-----------
extension Character
{
enum Kind
{
case Vowel,Consonant,Other
}
var kind: Kind
{
switch String(self).lowercaseString
{
case "a","e","i","o","u":
return .Vowel
case "b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z":
return .Consonant
default:
return .Other
}
}
}
func printLetterKinds(word: String)
{
print("'\(word)'由如下字母类型组成:")
//appendNewline换行打印
for character in word.characters
{
switch character.kind
{
//元音
case .Vowel:
print("元音字母 ",appendNewline: false)
//辅音
case .Consonant:
print("辅音字母 ",appendNewline: false)
case .Other:
print("其他 ",appendNewline: false)
}
}
print("\n",appendNewline: false)
}
printLetterKinds("Hello")