Protocol Extensions क्या हैं
एक protocol extension आपको असली code को एक बार, बिल्कुल checklist पर ही, लिखने देता है, ताकि conform करने वाला हर type अपने आप वह behavior free में पाए।
Syntax
protocol ProtocolName {
var property: Type { get }
}
extension ProtocolName {
func method() -> ReturnType { // default implementation
return value
}
}
Providing a Default Implementation
एक protocol extension सीधे एक method implement कर सकता है, इसलिए कोई भी conforming type उसे खुद लिखे बिना अपने आप वह behavior पाता है।
उदाहरण: Providing a Default Implementation
// Define a protocol `Greetable` describing required behavior
protocol Greetable {
var name: String { get }
}
// Extend `Greetable` with additional functionality
extension Greetable {
// Define the function `greet` with no parameters and returning a String
func greet() -> String {
// Return `"Hello, \(name)!"` from this function
return "Hello, \(name)!"
}
}
// Define a struct `Visitor`
struct Visitor: Greetable {
var name: String
}
// Declare a constant `visitor`, set to `Visitor(name: "Sam")`
let visitor = Visitor(name: "Sam")
// Print `visitor.greet()` to the console
print(visitor.greet())
Login to try C/C++/Java/PHP code in the editor
Overriding a Default Implementation
एक conforming type अब भी एक method का अपना implementation प्रदान कर सकता है जिसे एक protocol extension पहले से define करता है, जो उस type के लिए priority लेता है।
उदाहरण: Overriding a Default Implementation
// Define a protocol `Greetable` describing required behavior
protocol Greetable {
var name: String { get }
}
// Extend `Greetable` with additional functionality
extension Greetable {
// Define the function `greet` with no parameters and returning a String
func greet() -> String {
// Return `"Hello, \(name)!"` from this function
return "Hello, \(name)!"
}
}
// Define a struct `Robot`
struct Robot: Greetable {
var name: String
// Define the function `greet` with no parameters and returning a String
func greet() -> String {
// Return `"BEEP \(name) BOOP"` from this function
return "BEEP \(name) BOOP"
}
}
// Declare a constant `robot`, set to `Robot(name: "Unit1")`
let robot = Robot(name: "Unit1")
// Print `robot.greet()` to the console
print(robot.greet())
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. #}
आम गलतियां
- यह मान लेना कि एक protocol extension का default implementation हमेशा चलता है; एक conforming type अब भी इसे अपने implementation से override कर सकता है।
- यह भूल जाना कि protocol extensions नए required members नहीं जोड़ सकते -- वे सिर्फ default implementations या पूरी तरह नए convenience methods प्रदान करते हैं।
- किसी protocol extension में stored properties जोड़ना, जो allowed नहीं है; सिर्फ computed properties और methods जोड़े जा सकते हैं।
चैप्टर सारांश
- एक protocol extension किसी method या computed property के लिए एक default implementation प्रदान करता है।
- कोई भी conforming type अपने आप default पाता है जब तक यह अपना खुद का implementation प्रदान न करे।
- Extensions किसी protocol में stored properties या नए required members नहीं जोड़ सकते।
- यह pattern कई conforming types में repeated boilerplate घटाता है।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: