メソッド(研究ノート)
5.1環境Xcodeの11.0 beta4 SWIFT
- 方法
- 構造、列挙、メソッドのクラスが定義されてもよい(インスタンスメソッド、タイプ方法)
- 方法の例(インスタンスメソッド)
この方法の例としては、インスタンスのみを単独で使用することはできません呼び出すことができます
class Counter { var count = 0 func increment() { count += 1 } func increment(by amount: Int) { count += amount } func reset() { count = 0 } } let counter = Counter() // the initial counter value is 0 counter.increment() // the counter's value is now 1 counter.increment(by: 5) // the counter's value is now 6 counter.reset() // the counter's value is now 0
self
暗黙の型の各インスタンスの属性は、特性を有します// 上面的increment()可写成如下形式 func increment() { self.count += 1 } // 如果方法的参数与实例相同时,就要显示的调用 self struct Point { var x = 0.0, y = 0.0 func isToTheRightOf(x: Double) -> Bool { return self.x > x } } let somePoint = Point(x: 4.0, y: 5.0) if somePoint.isToTheRightOf(x: 1.0) { print("This point is to the right of the line where x == 1.0") } // Prints "This point is to the right of the line where x == 1.0"
インスタンスメソッドは、使用タイプの値を変更する内
mutating
キーワードが非表示に新しいインスタンスを割り当てる方法を変更しself
、この新しい方法の終了は、インスタンスインスタンスがすでに存在している置換します// 枚举、结构体都是值类型 struct Point { var x = 0.0, y = 0.0 mutating func moveBy(x deltaX: Double, y deltaY: Double) { x += deltaX y += deltaY } } var somePoint = Point(x: 1.0, y: 1.0) somePoint.moveBy(x: 2.0, y: 3.0) print("The point is now at (\(somePoint.x), \(somePoint.y))") // Prints "The point is now at (3.0, 4.0)" // 不能用常量来调用 `mutating` 修饰的方法,因为常量不可修改 let fixedPoint = Point(x: 3.0, y: 3.0) fixedPoint.moveBy(x: 2.0, y: 3.0) // this will report an error
変数の方法
self
割り当て(変異メソッド内で自己に割り当て )// 上面的Point可以写成下面的形式 struct Point { var x = 0.0, y = 0.0 mutating func moveBy(x deltaX: Double, y deltaY: Double) { self = Point(x: x + deltaX, y: y + deltaY) } } // 枚举的可变方法 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 is now equal to .high ovenLight.next() // ovenLight is now equal to .off
- 型のメソッド
で
func
フロントキーワードstatic
またはclass
(サブクラスが唯一のクラスで使用することができ、かつ親の実装をオーバーライドしてもよい)変更され、プロセスのタイプself
ポイントタイプ自体class SomeClass { class func someTypeMethod() { // type method implementation goes here } } SomeClass.someTypeMethod()
struct LevelTracker { static var highestUnlockedLevel = 1 var currentLevel = 1 // static func unlock(_ level: Int) { if level > highestUnlockedLevel { highestUnlockedLevel = level } } // static func isUnlocked(_ level: Int) -> Bool { return level <= highestUnlockedLevel } // @discardableResult mutating func advance(to level: Int) -> Bool { if LevelTracker.isUnlocked(level) { currentLevel = level return true } else { return false } } } class Player { var tracker = LevelTracker() let playerName: String func complete(level: Int) { LevelTracker.unlock(level + 1) tracker.advance(to: level + 1) } init(name: String) { playerName = name } } var player = Player(name: "Argyrios") player.complete(level: 1) print("highest unlocked level is now \(LevelTracker.highestUnlockedLevel)") // Prints "highest unlocked level is now 2" // 创建一个新的player player = Player(name: "Beto") if player.tracker.advance(to: 6) { print("player is now on level 6") } else { print("level 6 has not yet been unlocked") } // Prints "level 6 has not yet been unlocked"