9.34. Swift method

发布时间 :2023-12-08 01:10:05 UTC      

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.

9.34.1. 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.

9.34.2. Grammar #

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

9.34.3. 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

9.34.4. 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

9.34.5. 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.

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.