方法是与某些特定类型相关联的函数。类、结构体、枚举都可以定义实例方法;实例方法为给定类型的实例封装了具体的任务与功能。类、结构体、枚举也可以定义类型方法;类型方法与类型本身相关联。类型方法与 Objective-C 中的类方法(class methods)相似。
实例方法
class Counter {
var count = 0
func increment() {
count += 1
}
func incrementBy(amount: Int) {
count += amount
}
func reset() {
count = 0
}
}
//用点语法调用
let counter = Counter()
counter.increment() // 1
counter.incrementBy(5) // 6
counter.reset() // 0
//方法的局部参数名和外部参数名称
class Counter1 {
var count: Int = 0
func incrementBy(amount: Int, numberOfTimes: Int) {
count += amount * numberOfTimes
}
func incrementBy(amount amount: Int,_ numberOfTimes: Int) {
count += amount * numberOfTimes
}
}
let counter1 = Counter1()
counter1.incrementBy(5, numberOfTimes: 3) // 15
//修改方法的外部参数名
counter1.incrementBy(amount: 5, 3) //30
//self属性
class Counter2 {
var count: Int = 0
func increment() {
self.count += 1
}
}
//用self消除方法参数 x和实例属性 x 之间的歧义
struct Point {
var x = 0.0, y = 0.0
func isToTheRightOfX(x: Double) -> Bool {
return self.x > x
}
}
let somePoint = Point(x: 4.0, y:5.0)
if somePoint.isToTheRightOfX(1.0) {
print("This point is to the right of the line where x == 1.0")
}
//在实例方法中修改值类型
struct Point1 {
var x = 0.0, y = 0.0
mutating func moveByX(x deltaX: Double,y deltaY: Double) {
x += deltaX
y += deltaY
}
}
var somePoint1 = Point1(x: 1.0, y: 1.0)
somePoint1.moveByX(x: 3.0, y: 4.0)
print("The point is now at (\(somePoint1.x), \(somePoint1.y))")
//但是不能改变常量类型的值类型
let somePoint11 = Point1(x: 1.0, y: 1.0)
//somePoint11.moveByX(x: 3.0, y: 4.0) //这会报错
//在可变方法中给self赋值
struct Point2 {
var x = 0.0, y = 0.0
mutating func moveByX(x deltaX: Double, y deltaY: Double) {
self = Point2(x: x + deltaX, y: y + deltaY)
}
}
//枚举可变方法,self设置为统一枚举类型中不同成员
enum TriStateSwitch {
case Off, Low, High
mutating func next() {
switch self {
case .Off:
self = Low
case .Low:
self = High
case .High:
self = Off
}
}
}
var ovenLight = TriStateSwitch.Low
ovenLight.next() // .High
ovenLight.next() // .Off
类型方法
//OC中的类方法,可以用类名直接调用
class SomeClass {
class func someTypeMethod() {
//type method implementation goes here
}
}
SomeClass.someTypeMethod()
struct LevelTracker {
static var highestUnlockedLevel = 1
static func unlockLevel(level: Int) {
if level > highestUnlockedLevel {
highestUnlockedLevel = level
}
}
static func levelIsUnlocked(level: Int) -> Bool {
return level <= highestUnlockedLevel
}
var currentLevel = 1
mutating func advanceToLevel(level: Int) -> Bool {
if LevelTracker.levelIsUnlocked(level) {
currentLevel = level
return true
} else {
return false
}
}
}
class Player {
var tracker = LevelTracker()
let playerName: String
init(name: String) {
playerName = name
}
func completedLevel(level: Int) {
LevelTracker.unlockLevel(level + 1)
tracker.advanceToLevel(level + 1)
}
}
var player = Player(name: "Tom")
player.completedLevel(1)
print("highest unlocked level is now \(LevelTracker.highestUnlockedLevel)")
player = Player(name: "Beto")
if player.tracker.advanceToLevel(6) {
print("player is now on level 6")
} else {
print("level 6 has not yet been unlocked")
}
原文链接,Swift好难理解起来真是,,,费脑,,,
--EOF--