9.34. Swift method

发布时间 : 2025-10-25 13:33:52 UTC      

Page Views: 9 views

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.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。