Swift 方法
在Swift 4语言中,与特定类型相关联的函数被称为方法。在Objective C中,使用类来定义方法,而Swift 4语言为用户提供了在类、结构和枚举中定义方法的灵活性。
实例方法
在Swift 4语言中,可以通过实例方法来访问类、结构和枚举实例。
实例方法提供功能:
- 访问和修改实例属性
 - 与实例需求相关的功能
 
实例方法可以在{}花括号内编写。它隐式地访问类型实例的方法和属性。当调用特定类型的实例时,它将获得对该特定实例的访问权限。
语法
func funcname(Parameters) -> returntype {
   Statement1
   Statement2
   ---
   Statement N
   return parameters
}
示例
class calculations {
   let a: Int
   let b: Int
   let res: Int
   init(a: Int, b: Int) {
      self.a = a
      self.b = b
      res = a + b
   }
   func tot(c: Int) -> Int {
      return res - c
   }
   func result() {
      print("Result is: \(tot(c: 20))")
      print("Result is: \(tot(c: 50))")
   }
}
let pri = calculations(a: 600, b: 300)
pri.result()
当我们在playground中运行上述程序时,我们会得到以下结果:
Result is: 880
Result is: 850
Class Calculations定义了两个实例方法−
- init()定义了将两个数字a和b相加并将其存储在变量’res’中的方法
 - tot()用于将传递的’c’值减去’res’
 
最后,调用计算方法以及a和b的值进行打印。实例方法通过’.’语法访问
本地和外部参数名
Swift 4函数描述了变量的本地和全局声明。同样,Swift 4方法的命名约定也类似于Objective-C。但是函数和方法的本地和全局参数名称声明的特性是不同的。Swift 4中的第一个参数由介词名称’with’,’for’和’by’来引用,以便于访问命名约定。
Swift 4通过将第一个参数名称声明为本地参数名,将其余的参数名称声明为全局参数名来提供方法的灵活性。这里,’no1’被Swift 4方法声明为本地参数名。使用’no2’进行全局声明,并在整个程序中进行访问。
class division {
   var count: Int = 0
   func incrementBy(no1: Int, no2: Int) {
      count = no1 / no2
      print(count)
   }
}
let counter = division()
counter.incrementBy(no1: 1800, no2: 3)
counter.incrementBy(no1: 1600, no2: 5)
counter.incrementBy(no1: 11000, no2: 3)
当我们在playground中运行上述程序时,我们得到以下结果:
当我们在playground中运行上述程序时,我们得到以下结果−
600
320
3666
外部参数名使用“#”和“_”符号
尽管Swift 4方法为局部声明提供了第一个参数名,但用户可以将参数名从局部声明修改为全局声明。可以通过在第一个参数名前加上“#”符号来实现。通过这样做,第一个参数可以在整个模块中全局访问。
当用户需要使用外部名称访问后续的参数名时,可以使用“_”符号覆盖方法名称。
class multiplication {
   var count: Int = 0
   func incrementBy(no1: Int, no2: Int) {
      count = no1 * no2
      print(count)
   }
}
let counter = multiplication()
counter.incrementBy(no1: 800, no2: 3)
counter.incrementBy(no1: 100, no2: 5)
counter.incrementBy(no1: 15000, no2: 3)
当我们在playground上运行上述程序时,我们会得到以下结果 −
2400
500
45000
在方法中的Self属性
方法对于其定义的所有类型实例都有一个隐含的属性,称为“self”。“Self”属性用于引用所定义方法的当前实例。
class calculations {
   let a: Int
   let b: Int
   let res: Int
   init(a: Int, b: Int) {
      self.a = a
      self.b = b
      res = a + b
      print("Inside Self Block: \(res)")
   }
   func tot(c: Int) -> Int {
      return res - c
   }
   func result() {
      print("Result is: \(tot(c: 20))")
      print("Result is: \(tot(c: 50))")
   }
}
let pri = calculations(a: 600, b: 300)
let sum = calculations(a: 1200, b: 300)
pri.result()
sum.result()
当我们在playground上运行上述程序时,我们得到以下结果-
Inside Self Block: 900
Inside Self Block: 1500
Result is: 880
Result is: 850
Result is: 1480
Result is: 1450
从实例方法修改值类型
在Swift 4语言中,结构体和枚举属于值类型,不能通过其实例方法进行更改。然而,Swift 4语言提供了通过’mutating’行为来修改值类型的灵活性。Mutate会在实例方法中进行任何更改,并在方法执行后返回原始形式。此外,通过’self’属性,在其隐式函数中创建新实例,并在执行后替换现有方法。
struct area {
   var length = 1
   var breadth = 1
   func area() -> Int {
      return length * breadth
   }
   mutating func scaleBy(res: Int) {
      length *= res
      breadth *= res
      print(length)
      print(breadth)
   }
}
var val = area(length: 3, breadth: 5)
val.scaleBy(res: 3)
val.scaleBy(res: 30)
val.scaleBy(res: 300)
当我们在playground上运行以上程序时,我们得到以下结果−
9
15
270
450
81000
135000
自动变异方法的自我属性
与 ‘self’ 属性结合的变异方法将一个新的实例分配给定义的方法。
struct area {
   var length = 1
   var breadth = 1
   func area() -> Int {
      return length * breadth
   }
   mutating func scaleBy(res: Int) {
      self.length *= res
      self.breadth *= res
      print(length)
      print(breadth)
   }
}
var val = area(length: 3, breadth: 5)
val.scaleBy(res: 13)
当我们在playground中运行上述程序时,我们会得到以下结果。-
39
65
类型方法
当调用特定实例方法时,称之为实例方法;当方法调用特定类型的方法时,称之为“类型方法”。类的类型方法是用 ‘func’ 关键字定义的,而结构体和枚举的类型方法是在 ‘func’ 关键字之前用 ‘static’ 关键字定义的。
通过 ‘.’ 语法调用和访问类型方法,而不是调用特定的实例。
class Math {
   class func abs(number: Int) -> Int {
      if number < 0 {
         return (-number)
      } else {
         return number
      }
   }
}
struct absno {
   static func abs(number: Int) -> Int {
      if number < 0 {
         return (-number)
      } else {
         return number
      }
   }
}
let no = Math.abs(number: -35)
let num = absno.abs(number: -5)
print(no)
print(num)
当我们在Playground中运行上述程序时,我们得到以下结果。
35
5
极客教程