- 方法是关联了特定类型的函数。类,结构体,枚举都可以定义实例方法
class Counter {
var count = 0;
func increment() {
count += 1
}
func increment(by amount: Int) {
count += amount
}
func reset() {
count = 0
}
}
let counter = Counter()
counter.increment()
counter.increment(by: 2)
print(counter.count)
counter.reset()
print(counter.count)
- self属性 普通的函数调用中是不需要写的系统可以判断,但是在你的函数的形式参数和你的类的属性相同的时候self就派上用场了。
struct Point {
var x = 0.0, y = 0.0
func isToTheRightOf(x: Double) -> Bool {
return self.x > x
}
}
// 通常我们都知道结构体和枚举都是值类型,类是引用类型,所以在结构体和枚举的实例方法中修改属性需要用到mutating
struct Mooshroom {
var name = "moo", age = 25
// func replaceName(name: String, age: Int) {
// self.name = name // 报错 Cannot assign to property: 'self' is immutable
// }
// 这样就不会报错啦
mutating func replaceName(name: String, age: Int) {
self.name = name // 这样就不会报错啦
self.age = age
}
}
struct Mooshroom1 {
var name = "moo", age = 25
mutating func replaceName(name: String, age: Int) {
self = Mooshroom1(name: name, age: age)
}
}
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()
ovenLight.next()
ovenLight.next()
// 如果你想访问类的属性 或者结构体的属性 就需要使用 static 或者 class 前缀来限制它:
// static var highestUnlockedLevel = 1
struct LevelTracker {
static var highestUnlockedLevel = 1
var currentLevel = 1
static func unlock(_ level: Int) {
if level > highestUnlockedLevel { highestUnlockedLevel = level }
}
static func isUnlock(_ level: Int) -> Bool {
return level <= highestUnlockedLevel
}
@discardableResult // 忽略返回值
mutating func advance(to level: Int) -> Bool {
if LevelTracker.isUnlock(level) {
currentLevel = level
return true
} else {
return false
}
}
}
var levelTracker = LevelTracker(currentLevel: 1)
levelTracker.advance(to: 2)
网友评论