Enums में Methods
Enums में भी अपने खुद के actions हो सकते हैं, बिल्कुल structs और classes की तरह, हर option को यह जानने देते हुए कि कैसे behave करना है।
Syntax
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
// 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())
Login to try C/C++/Java/PHP code in the editor
A Mutating Method that Changes Cases
किसी enum पर एक mutating method self को पूरी तरह एक अलग case में reassign कर सकता है, जो state transitions model करने का तरीका है।
उदाहरण: A Mutating Method that Changes Cases
// 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)
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- यह भूल जाना कि एक enum method जिसे
selfको एक अलग case में बदलना है उसेmutatingचिह्नित किया जाना चाहिए, बिल्कुल एक struct की तरह। - हर case के लिए behavior compute करने के लिए एक बड़ा external
switchfunction लिखना जब enum पर ही defined एक method,selfको एक switch में उपयोग करते हुए, ज़्यादा natural होता। - यह न समझना कि 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: