← Back to Swift Course | Chapter 10: Enums | Lesson 5 of 6

Enums में Methods

Enums में भी अपने खुद के actions हो सकते हैं, बिल्कुल structs और classes की तरह, हर option को यह जानने देते हुए कि कैसे behave करना है।
Syntax
swift
enum EnumName {
    case first, second

    func methodName() -> ReturnType {
        switch self {
        case .first: return value
        case .second: return value
        }
    }

    mutating func change() { self = .second }
}

Defining a Method on an Enum

एक enum method switch self उपयोग करके अलग behavior produce कर सकता है इस आधार पर कि instance अभी कौन सा case है।

उदाहरण: Defining a Method on an Enum

markup
// Define an enum `TrafficLight` listing its possible cases
enum TrafficLight {
    // Enum case(s): `red, yellow, green`
    case red, yellow, green
    // Define the function `instruction` with no parameters and returning a String
    func instruction() -> String {
        // Switch on `self` and branch by case
        switch self {
        case .red: return "Stop"
        case .yellow: return "Slow down"
        case .green: return "Go"
        }
    }
}
// Declare a constant `light`, set to `TrafficLight.green`
let light = TrafficLight.green
// Print `light.instruction()` to the console
print(light.instruction())

A Mutating Method that Changes Cases

किसी enum पर एक mutating method self को पूरी तरह एक अलग case में reassign कर सकता है, जो state transitions model करने का तरीका है।

उदाहरण: A Mutating Method that Changes Cases

markup
// Define an enum `TrafficLight` listing its possible cases
enum TrafficLight {
    // Enum case(s): `red, yellow, green`
    case red, yellow, green
    mutating func next() {
        // Switch on `self` and branch by case
        switch self {
        // Enum case(s): `.red: self = .green`
        case .red: self = .green
        // Enum case(s): `.green: self = .yellow`
        case .green: self = .yellow
        // Enum case(s): `.yellow: self = .red`
        case .yellow: self = .red
        }
    }
}
// Declare a mutable variable `light`, initialized to `TrafficLight.red`
var light = TrafficLight.red
light.next()
// Print `light` to the console
print(light)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि एक enum method जिसे self को एक अलग case में बदलना है उसे mutating चिह्नित किया जाना चाहिए, बिल्कुल एक struct की तरह।
  2. हर case के लिए behavior compute करने के लिए एक बड़ा external switch function लिखना जब enum पर ही defined एक method, self को एक switch में उपयोग करते हुए, ज़्यादा natural होता।
  3. यह न समझना कि enums में computed properties और static methods भी हो सकते हैं, बिल्कुल structs और classes की तरह।
चैप्टर सारांश
  • Enums instance methods define कर सकते हैं, बिल्कुल structs और classes की तरह।
  • एक method जो self को एक अलग case में reassign करता है उसे mutating चिह्नित किया जाना चाहिए।
  • Methods अक्सर हर case के लिए behavior vary करने के लिए internally switch self उपयोग करते हैं।
  • Enums में static methods और computed properties भी हो सकते हैं।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.