Painted levels: being fostered fostered fostered

Tags: “iOS” “Swift 5.1” “Instance method” “Class method” “Mutating


methods

Methods are functions associated with a particular type. Classes, structs, and enumerations in Swift can all define instance methods that encapsulate specific tasks and functions for handling specific types of instances. Classes, structs, and enumerations can also define type methods that are associated with the type itself. Structs and enumerations can define methods in Swift, a major difference from C and Objective-C.

Instance methods

Instance methods are functions that belong to instances of a particular class, structure, or enumerated type. Instance methods have exactly the same syntax as functions. Instance methods have implicit access to all other instance methods and properties of that type. Instance methods can only be called on specific instances of their type.

class Counter { var count = 0 func increment() { count += 1 } func increment(by amount: Int) {count += amount} func reset() {count = 0}} < count : 0 counter.increment()//! <count : 1 counter.increment(by: 5)//! < count : 6 counter.reset()//! < count : 0Copy the code

selfattribute

Each instance of a type has an implicit attribute of self, which is exactly equivalent to the instance itself. References the current instance object in its own instance method using the self attribute. Most of the time for a particular type, we don’t need to use the self attribute in the instance method to refer to the instance’s attribute or call other methods. Because when we don’t explicitly refer to a known property or method with self, Swift automatically inferences to an instance of the current type. But when the parameter name of our instance method is the same as the attribute name, inside the method, the parameter name takes precedence. If you need to use attributes, you need to explicitly refer to the attributes using self to distinguish the parameters.

Instance method to modify the value type

Structures and enumerations are value types. By default, the properties of a value type cannot be modified in a sample method. If you need to modify enumeration or structure in the specific methods defined in the attribute, use the mutating keyword in enumeration or structure defined in the method of func keywords, before that the method can modify the enumeration in the method or the properties of the structure, and when at the end of the method, it does any change will write back to the original data structure. The method can also assign an entirely new instance to its implicit self attribute, and the new instance represented by self will replace the existing instance at the end of the method.

Struct Point {var x = 0.0, y = 0.0 // Mutating func moveBy(x deltaX: Double, y deltaY: Double) Double) {x += deltaX y += deltaY}} var somePoint = Point(x: 1.0, y: 1.0) somePoint.moveBy(x: 2.0, y: 1.0) 3.0) print (" current point: (\ somePoint. (x), \ [somePoint. Y)) ") / /! < span style = "max-width: 100%; clear: both;Copy the code

The type ofmutating Method gives implicitselfAttribute assignment

Value type: structure

Struct Point {var x = 0.0, y = 0.0 // Mutating func moveBy(x deltaX: Double, y deltaY: Double) {self = Point. Init (x: x + deltaX, y: Double) {self = Point. y + deltaY) } }Copy the code

Value type: Enumeration type

enum SizeType:Int,CaseIterable { case big,middle,small mutating func nextCase() { switch self { case .big: self = .middle case .middle: self = .small case .small: Use let sizeType = sizeType. AllCases for var item in sizeType {print(" current item:\(item)") Item.nextcase () print(" after calling method :\(item)")}Copy the code

Type method

Type method: a method called using the type itself rather than an instance of the type. Type method representation: add the static keyword before the func keyword; If the current type is a class type, the class keyword can also be used. Using the class keyword for a class type allows subclasses to override the method implementation of the parent class.

Class SomeClass {// define class func someTypeMethod() {// implement the class method}} // call SomeClass. SomeTypeMethod ()Copy the code

In type methods, the implicit self attribute refers to the type itself rather than to an instance of the type. This means that we can use the self attribute to disambiguate class attributes and type method parameters.

The declared property of the function or method@discardableResult

@discardableResult

Applying this property before a function or method declaration indicates that although the function has a return value, the compiler should not generate a warning if the return value is not used.

override init() { super.init() instanceMethod() } @discardableResult func instanceMethod() -> String { return "The return value is a string, but the call will not use it, use the declaration attribute '@discardableresult' to avoid compiler warnings"}Copy the code

The return value is a string, but it is not used in the call. Use the declaration attribute @discardableresult to avoid compiler warnings.

Resources: Swift 5.1 official programming guide


Recommended articles:

Swift 5.1 (10) – Properties iOS App Background keep-active Use CGAffineTransform iN Swift iOS performance monitoring (1) — CPU power monitoring iOS performance monitoring (2) — Main thread lag monitoring iOS performance monitoring (3) — Add animation to the view with SwiftUI write a simple page with SwiftUI iOS App startup Optimization (3) — make your own tool to monitor the startup Time of the App — use “Time” “Profiler” tool monitoring App startup time iOS App startup optimization (a) – understand the App startup process qidance Weekly