> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Swift Instance Method

An instance method is a function encapsulated within a specific class, structure, or enumeration that operates on a specific instance of that type. It defines the behavior associated with an object or value, possessing implicit access to the instance's stored properties, computed properties, and other instance methods.

## Syntax and Context

Instance methods are declared using the `func` keyword within the lexical scope of the type definition. They are invoked using dot syntax on an instantiated object.

```swift theme={"dark"}
class Counter {
    var count: Int = 0
    
    func increment(by amount: Int) {
        count += amount
    }
}

let myCounter = Counter()
myCounter.increment(by: 5)
```

## The Implicit `self` Property

Every instance of a type has an implicit property called `self`, which is a reference to the instance itself. Within an instance method, `self` is used to access properties and methods of the current instance. While Swift allows omitting `self` when accessing properties, it is required when disambiguating between an instance property and a method parameter with the same name.

```swift theme={"dark"}
class Point {
    var x: Double = 0.0
    
    func updateX(x: Double) {
        // 'self.x' refers to the property, 'x' refers to the parameter
        self.x = x 
    }
}
```

## Value Types and the `mutating` Keyword

In Swift, structures and enumerations are value types. By default, the properties of a value type cannot be modified from within its instance methods.

To enable property mutation, the instance method must be explicitly declared with the `mutating` keyword. Under the hood, a `mutating` method receives the implicit `self` property as an `inout` parameter. This allows the method to mutate the properties or even assign an entirely new instance to `self`, overwriting the existing value in memory.

```swift theme={"dark"}
struct StateMachine {
    var state: Int = 0
    
    mutating func advance() {
        state += 1
    }
    
    mutating func reset() {
        self = StateMachine(state: 0) // Reassigning self
    }
}
```

*Note: Classes are reference types; therefore, their instance methods can mutate properties without the `mutating` keyword.*

## Method Dispatch Mechanisms

The way Swift invokes an instance method depends on the type it belongs to and its declaration modifiers:

1. **Static (Direct) Dispatch:** Used for instance methods on value types (structs and enums), `final` classes, and methods declared in class extensions. The compiler determines the memory address of the method at compile time, resulting in highly optimized, inlineable execution.
2. **Dynamic (Table) Dispatch:** Used for standard instance methods in non-final classes. Swift uses a Virtual Method Table (vtable) at runtime to look up the correct implementation of the method, allowing for inheritance and method overriding.
3. **Message Dispatch:** Used when an instance method is marked with `@objc dynamic`. The method invocation is deferred to the Objective-C runtime using `objc_msgSend`.

## Instance Methods as Closures

In Swift, instance methods are essentially curried functions. You can reference an instance method without invoking it, either bound to a specific instance or unbound.

**Bound Reference:**
Referencing a method on a specific instance yields a closure that captures the instance.

```swift theme={"dark"}
let counter = Counter()
let boundMethod: (Int) -> Void = counter.increment(by:)
boundMethod(10) // Executes on 'counter'
```

**Unbound Reference:**
Referencing a method directly on the type yields a closure where the first parameter is the instance itself.

```swift theme={"dark"}
let unboundMethod: (Counter) -> (Int) -> Void = Counter.increment(by:)
let anotherCounter = Counter()
unboundMethod(anotherCounter)(5) // Explicitly passes the instance
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Swift Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
