码迷,mamicode.com
首页 > 编程语言 > 详细

Swift4 方法,文档代码

时间:2018-04-07 17:32:15      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:int   ati   代码   this   blog   指定   port   UNC   var   

方法

苹果官方文档 Methods

苹果文档翻译 方法

方法 是关联了特定类型的函数。

实例方法

实例方法 是属于特定类实例、结构体实例或者枚举实例的函数。

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属性

没有显式地写出 self,Swift会在你于方法中使用已知属性或者方法的时候假定你是调用了当前实例中的属性或者方法。

但是,当一个实例方法的形式参数名与实例中某个属性拥有相同的名字的时候,形式参数名具有优先权,可以使用 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,需要变量实例。

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)"

let fixedPoint = Point(x: 3.0, y: 3.0)
fixedPoint.moveBy(x: 2.0, y: 3.0)
// this will report an error

在异变方法里指定自身

上例可写为:

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)
    }
}

枚举中设置隐含的 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()
// ovenLight is now equal to .high
ovenLight.next()
// ovenLight is now equal to .off

类型方法

定义在类型本身调用的方法,这类方法被称作类型方法。你可以通过在 func关键字之前使用 static关键字来明确一个类型方法。

类型方法和实例方法一样使用点语法调用。不过,你得在类上调用类型方法,而不是这个类的实例。

class SomeClass {
    class func someTypeMethod() {
        // type method implementation goes here
    }
}
SomeClass.someTypeMethod()

定义了一个叫做 LevelTracker的结构体,它通过不同的等级或者阶层来追踪玩家的游戏进度。这是一个单人游戏,但是可以在一个设备上储存多个玩家的信息。

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
        }
    }
}

LevelTracker结构体与 Player类共同使用来追踪和更新每一个玩家的进度:

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(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"

代码详解查询文档

Swift4 方法,文档代码

标签:int   ati   代码   this   blog   指定   port   UNC   var   

原文地址:https://www.cnblogs.com/ccjoy/p/8733673.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!