Swift method


Release date:2023-12-01 Update date:2023-12-08 Editor:admin View counts:212

Label:

Swift method

Swift methods are functions associated with certain types

In Objective-C, a class is the only type that can define a method. But in Swift, you can not only choose whether or not to define a class / structure / enumeration, but also have the flexibility to define methods on the type (class / structure / enumeration) you create.

Example method

In the Swift language, an instance method is a method that belongs to a particular class, structure, or enumerated type instance.

The instance method provides the following methods:

  • You can access and modify instance properties

  • Provide functions related to the purpose of the instance

The instance method is written between the braces ({}) of the type to which it belongs.

An instance method can implicitly access all other instance methods and properties of the type to which it belongs.

An instance method can only be called by a specific instance of the class to which it belongs.

An instance method cannot be called without an existing instance.

Grammar

func funcname(Parameters) -> returntype
{
    Statement1
    Statement2
    ……
    Statement N
    return parameters
}

Example

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 now 1
counter.increment()

// The current count is 6
counter.incrementBy(amount: 5)
print(counter.count)

// The count value is now 0
counter.reset()
print(counter.count)

The output of the above program execution is as follows:

6
0

Counter class defines three instance methods:

  • increment let the counter increment by 1

  • incrementBy(amount: Int) increments the counter by a specified integer value

  • reset resets the counter to 0.

Counter This class also declares a mutable property count which is used to keep track of the current counter value

Local parameter name and external parameter name of the method

Swift function parameters can have both a local name (used inside the function body) and an external name (used when calling the function

The methods in Swift are extremely similar to those in Objective-C . Just like in Objective-C , the name of a method in Swift usually points to the first parameter of the method with a preposition, such as with , for , by , and so on.

By default, Swift gives only the first parameter name of the method a local parameter name; by default, both the second and subsequent parameter names are global parameter names.

In the following example, ‘no1’ is declared as a local parameter name in swift. ‘No2’ is used for global declaration and accessed through 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 of the above program execution is as follows:

600
320
3666

Whether to provide external name settings

We force the addition of an external name to the first parameter to use thislocal name as an external name (the # sign was used before Swift 2.0).

On the contrary, we can also use an underline (_) to set the second and subsequent parameters without providing an external name.

Powered by TorCMS (https://github.com/bukun/TorCMS).