Swift 方法


Swift 方法是与某些特定类型相关联的函数

在 Objective-C 中,类是唯一能定义方法的类型。但在 Swift 中,你不仅能选择是否要定义一个类/结构体/枚举,还能灵活的在你创建的类型(类/结构体/枚举)上定义方法。

实例方法

在 Swift 语言中,实例方法是属于某个特定类、结构体或者枚举类型实例的方法。

实例方法提供以下方法:

  • 可以访问和修改实例属性

  • 提供与实例目的相关的功能

实例方法要写在它所属的类型的前后大括号({})之间。

实例方法能够隐式访问它所属类型的所有的其他实例方法和属性。

实例方法只能被它所属的类的某个特定实例调用。

实例方法不能脱离于现存的实例而被调用。

语法

  1. func funcname(Parameters) -> returntype
  2. {
  3. Statement1
  4. Statement2
  5. ……
  6. Statement N
  7. return parameters
  8. }

实例

  1. import Cocoa
  2.  
  3. class Counter {
  4. var count = 0
  5. func increment() {
  6. count += 1
  7. }
  8. func incrementBy(amount: Int) {
  9. count += amount
  10. }
  11. func reset() {
  12. count = 0
  13. }
  14. }
  15. // 初始计数值是0
  16. let counter = Counter()
  17.  
  18. // 计数值现在是1
  19. counter.increment()
  20.  
  21. // 计数值现在是6
  22. counter.incrementBy(amount: 5)
  23. print(counter.count)
  24.  
  25. // 计数值现在是0
  26. counter.reset()
  27. print(counter.count)

以上程序执行输出结果为:

  1. 6
  2. 0

Counter类定义了三个实例方法:

  • increment 让计数器按 1 递增;
  • incrementBy(amount: Int) 让计数器按一个指定的整数值递增;
  • reset 将计数器重置为0。

Counter 这个类还声明了一个可变属性 count,用它来保持对当前计数器值的追踪。

方法的局部参数名称和外部参数名称

Swift 函数参数可以同时有一个局部名称(在函数体内部使用)和一个外部名称(在调用函数时使用

Swift 中的方法和 Objective-C 中的方法极其相似。像在 Objective-C 中一样,Swift 中方法的名称通常用一个介词指向方法的第一个参数,比如:with,for,by等等。

Swift 默认仅给方法的第一个参数名称一个局部参数名称;默认同时给第二个和后续的参数名称为全局参数名称。

以下实例中 'no1' 在swift中声明为局部参数名称。'no2' 用于全局的声明并通过外部程序访问。

  1. import Cocoa
  2.  
  3. class division {
  4. var count: Int = 0
  5. func incrementBy(no1: Int, no2: Int) {
  6. count = no1 / no2
  7. print(count)
  8. }
  9. }
  10.  
  11. let counter = division()
  12. counter.incrementBy(no1: 1800, no2: 3)
  13. counter.incrementBy(no1: 1600, no2: 5)
  14. counter.incrementBy(no1: 11000, no2: 3)

以上程序执行输出结果为:

  1. 600
  2. 320
  3. 3666

是否提供外部名称设置

我们强制在第一个参数添加外部名称把这个局部名称当作外部名称使用(Swift 2.0前是使用 # 号)。

相反,我们呢也可以使用下划线(_)设置第二个及后续的参数不提供一个外部名称。
  1. import Cocoa
  2.  
  3. class multiplication {
  4. var count: Int = 0
  5. func incrementBy(first no1: Int, no2: Int) {
  6. count = no1 * no2
  7. print(count)
  8. }
  9. }
  10.  
  11. let counter = multiplication()
  12. counter.incrementBy(first: 800, no2: 3)
  13. counter.incrementBy(first: 100, no2: 5)
  14. counter.incrementBy(first: 15000, no2: 3)

以上程序执行输出结果为:

  1. 2400
  2. 500
  3. 45000

self 属性

类型的每一个实例都有一个隐含属性叫做self,self 完全等同于该实例本身。

你可以在一个实例的实例方法中使用这个隐含的self属性来引用当前实例。

  1. import Cocoa
  2.  
  3. class calculations {
  4. let a: Int
  5. let b: Int
  6. let res: Int
  7. init(a: Int, b: Int) {
  8. self.a = a
  9. self.b = b
  10. res = a + b
  11. print("Self 内: \(res)")
  12. }
  13. func tot(c: Int) -> Int {
  14. return res - c
  15. }
  16. func result() {
  17. print("结果为: \(tot(c: 20))")
  18. print("结果为: \(tot(c: 50))")
  19. }
  20. }
  21.  
  22. let pri = calculations(a: 600, b: 300)
  23. let sum = calculations(a: 1200, b: 300)
  24.  
  25. pri.result()
  26. sum.result()

以上程序执行输出结果为:

  1. Self 内: 900
  2. Self 内: 1500
  3. 结果为: 880
  4. 结果为: 850
  5. 结果为: 1480
  6. 结果为: 1450

在实例方法中修改值类型

Swift 语言中结构体和枚举是值类型。一般情况下,值类型的属性不能在它的实例方法中被修改。

但是,如果你确实需要在某个具体的方法中修改结构体或者枚举的属性,你可以选择变异(mutating)这个方法,然后方法就可以从方法内部改变它的属性;并且它做的任何改变在方法结束时还会保留在原始结构中。

方法还可以给它隐含的self属性赋值一个全新的实例,这个新实例在方法结束后将替换原来的实例。

  1. import Cocoa
  2.  
  3. struct area {
  4. var length = 1
  5. var breadth = 1
  6. func area() -> Int {
  7. return length * breadth
  8. }
  9. mutating func scaleBy(res: Int) {
  10. length *= res
  11. breadth *= res
  12. print(length)
  13. print(breadth)
  14. }
  15. }
  16.  
  17. var val = area(length: 3, breadth: 5)
  18. val.scaleBy(res: 3)
  19. val.scaleBy(res: 30)
  20. val.scaleBy(res: 300)

以上程序执行输出结果为:

  1. 9
  2. 15
  3. 270
  4. 450
  5. 81000
  6. 135000

在可变方法中给 self 赋值

可变方法能够赋给隐含属性 self 一个全新的实例。

  1. import Cocoa
  2.  
  3. struct area {
  4. var length = 1
  5. var breadth = 1
  6. func area() -> Int {
  7. return length * breadth
  8. }
  9. mutating func scaleBy(res: Int) {
  10. self.length *= res
  11. self.breadth *= res
  12. print(length)
  13. print(breadth)
  14. }
  15. }
  16. var val = area(length: 3, breadth: 5)
  17. val.scaleBy(res: 13)

以上程序执行输出结果为:

  1. 39
  2. 65

类型方法

实例方法是被类型的某个实例调用的方法,你也可以定义类型本身调用的方法,这种方法就叫做类型方法。

声明结构体和枚举的类型方法,在方法的func关键字之前加上关键字static。类可能会用关键字class来允许子类重写父类的实现方法。

类型方法和实例方法一样用点号(.)语法调用。

  1. import Cocoa
  2.  
  3. class Math
  4. {
  5. class func abs(number: Int) -> Int
  6. {
  7. if number < 0
  8. {
  9. return (-number)
  10. }
  11. else
  12. {
  13. return number
  14. }
  15. }
  16. }
  17.  
  18. struct absno
  19. {
  20. static func abs(number: Int) -> Int
  21. {
  22. if number < 0
  23. {
  24. return (-number)
  25. }
  26. else
  27. {
  28. return number
  29. }
  30. }
  31. }
  32.  
  33. let no = Math.abs(number: -35)
  34. let num = absno.abs(number: -5)
  35.  
  36. print(no)
  37. print(num)

以上程序执行输出结果为:

  1. 35
  2. 5