//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
/*---------------------Methods 方法----------------------*/
//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
//方法参数可以有两个名字, 一个名字在方法内部使用, 一个在调用方法的时候使用.
//如 increment(by amout: Int) amout 内部使用 by 外部使用
/************** self *****************/
// self 属性 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")
//This point is to the right of the line where x == 1.0
}
//上面的例子 在方法 `isToTheRightOf` 中 x 是方法参数 self.x 是Point的属性
// 这里使用self 单纯的是为了区分 属性和方法参数
// 如果在方法中 使用 x 视为方法参数, 在方法中 和属性同名的方法参数优先级大于属性
/**************方法突变*****************/
// 使用方法突变 更改值类型 的属性
// 默认情况下 枚举,结构体等值类型的实类方法中不能修改自己的属性
// 方法突变可以帮你实现这一点. 只要在 方法前面添加 mutatiing 关键字
struct PointS {
var x = 0.0, y = 0.0
mutating func moveBy(x deltaX: Double, y deltaY: Double){
x += deltaX
y += deltaY
}
}
var somePointS = PointS(x: 1.0, y: 1.0)
somePointS.moveBy(x:2.0, y:3.0)
print("The point is now at (\(somePointS.x), \(somePointS.y))")
// The point is now at (3.0, 4.0)
// 打印结果表示, 结构体属性被改变
// 事实上, mutating 修饰的方法中 重新创建了一个 PointS,
// 并且规定这个新建的PointS 的属性可以被更改, 然后返回这个新的PointS
// 注意: 只有用 var 修饰的结构体,枚举 的方法有 修饰词 mutating 才能改变属性
// 用 let 修饰的结构体,枚举 他们的属性无论如何都不会被更改.
/*********方法突变中使用 self ********/
struct PointM{
var x = 0.0, y = 0.0
mutating func moveBy(x deltaX: Double, y deltaY: Double){
self = PointM(x: x + deltaX, y: y + deltaY)
}
}
enum TriStateSwitch{
case off, low, hig
mutating func next(){
switch self{
case .off:
self = .low
case .low:
self = .hig
case .hig:
self = .off
}
}
}
var ovenLight = TriStateSwitch.low
ovenLight.next()// .hig
ovenLight.next() // .off
// 修改 枚举值 自身的状态.
/*************** 类方法 **************/
// 类方法 被 关键字 struct 和 calss 修饰
// 类方法 支持 类, 枚举, 结构体
// 用点语法调用方法 类名.方法名
class SomeClass{
class func someTypeMethod(){
}
}
SomeClass.someTypeMethod()
// 类方法中 self 代表这个类本身
// 实类方法中 self 代表这个实类
// 这是一个玩家等级结构体
// 第一次游戏开始 等级为 1
// 每当玩过一级 这个设备上所有玩家就解锁下一级
struct LevelTracker {
static var highestUnlockedLevel = 1
var currentLevel = 1
//解锁关卡
static func unlock(_ level: Int){
if level > highestUnlockedLevel{
// 类属性可以在不加 mutating 的情况下更改
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)")
//highest unlocked level is now 2
player = Player(name: "Beto")
if player.tracker.advance(to: 6){//玩家选择第6关
print("player is now on level 6")
} else {
print("level 6 has not yet been unlocked")
}
//level 6 has not yet been unlocked
swift - method 方法
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- swift语言 使用method_exchangeImplementations 交换原生方法不行了 ,fuck ...
- 创建一个实例对象或者访问一些全局类数据,最好使用类方法。类方法不能直接使用实例变量。 NSString *stri...