English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Swift methods are functions associated with certain specific types
In Objective-In C, classes are the only type that can define methods. But in Swift, you can not only choose whether to define a class/Structures/Enumerations, and can be flexibly defined in the types you create (classes/Structures/Define methods on enumerations.
In Swift, example methods are methods of a specific class, structure, or enumeration type example.
Example methods provide the following methods:
Can access and modify example properties
Provide functions related to the purpose of the example
Example methods should be written between the curly braces ({}) of the type they belong to.
Example methods can implicitly access all other methods and properties of the type they belong to.
Example methods can only be called by a specific instance of the class they belong to.
Example methods cannot be called independently of existing examples.
func funcname(Parameters) -> returntype { Statement1 Statement2 …… Statement N return parameters }
import Cocoa class Counter { var count = 0 func increment() { count +}} 1 } func incrementBy(amount: Int) { count += amount } func reset() { count = 0 } } // The initial count value is 0 let counter = Counter() // The count value is now1 counter.increment() // The count value is now6 counter.incrementBy(amount: 5) print(counter.count) // The count value is now 0 counter.reset() print(counter.count)
The output result of the above program is:
6 0
Counter class defines three example methods:
Counter This class also declares a mutable property countto keep track of the current counter value.
Swift function parameters can have both a local name (used within the function body) and an external name (used when calling the function
methods in Swift-C methods are extremely similar. Like in Objective-In C, just like in Swift, the name of a method is usually used with a preposition to point to the first parameter, such as: with, for, by, etc.
By default, Swift only gives the first parameter of a method a local parameter name; by default, the second and subsequent parameters are given global parameter names.
In the following example, 'no1' is declared as a local parameter name in Swift. 'no2' is used for global declarations and accessed by external programs.
import Cocoa 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)
The output result of the above program is:
600 320 3666
We force to add an external name to the first parameter to use this local name as an external name (Swift 2.0 before is using the # symbol).
On the contrary, we can also use an underscore (_ ) to set the second and subsequent parameters without providing an external name.import Cocoa class multiplication { var count: Int = 0 func incrementBy(first no1: Int, no2: Int) { count = no1 * no2 print(count) } } let counter = multiplication() counter.incrementBy(first: 800, no2: 3) counter.incrementBy(first: 100, no2: 5) counter.incrementBy(first: 15000, no2: 3)
The output result of the above program is:
2400 500 45000
Every instance of a type has an implicit property called self, which is completely equivalent to the instance itself.
You can use the implicit self property within an instance's instance method to refer to the current instance.
import Cocoa 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("Self inside: \(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()
The output result of the above program is:
Self inside: 900 Self inside: 1500 Result is: 880 Result is: 850 Result is: 1480 Result is: 1450
Structures and enumerations are value types in Swift. Generally, the properties of value types cannot be modified within their instance methods.
However, if you really need to modify the properties of a structure or enumeration in a specific method, you can choose to mutate (mutating) this method, and then the method can change its properties from within; and any changes it makes will be retained in the original structure at the end of the method.
Methods can also assign a completely new instance to the implicit self property, which will replace the original instance after the method ends.
import Cocoa 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)
The output result of the above program is:
9 15 270 450 81000 135000
Mutable methods can assign a completely new instance to the implicit property self.
import Cocoa 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)
The output result of the above program is:
39 65
Example methods are methods called on an instance of a type, but you can also define methods that the type itself calls, and these methods are called type methods.
Type methods are declared with the static keyword before the func keyword in a struct or enum. A class may use the class keyword to allow subclasses to override the implementation of a parent class.
Type methods are called using the dot (.) syntax, just like instance methods.
import Cocoa 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)
The output result of the above program is:
35 5