Protocol के Basics
एक protocol एक checklist जैसा है उन चीज़ों की जो एक type वादा करता है कर सकता है, बिल्कुल यह कहे बिना कि यह इन्हें कैसे करता है।
Syntax
protocol ProtocolName {
var property: Type { get }
func method() -> ReturnType
}
struct TypeName: ProtocolName {
var property: Type
func method() -> ReturnType { return value }
}
Defining a Protocol
एक protocol required properties और methods declare करता है जिन्हें किसी भी conforming type को implement करना चाहिए, बिना खुद एक implementation प्रदान किए।
उदाहरण: Defining a Protocol
// Define a protocol `Greetable` describing required behavior
protocol Greetable {
var name: String { get }
// Define the function `greet` with no parameters and returning a String
func greet() -> String
}
// 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 boop, I am \(name)"` from this function
return "Beep boop, I am \(name)"
}
}
// Declare a constant `robot`, set to `Robot(name: "R2")`
let robot = Robot(name: "R2")
// Print `robot.greet()` to the console
print(robot.greet())
Login to try C/C++/Java/PHP code in the editor
Multiple Types Conforming to the Same Protocol
अलग-अलग, unrelated types सभी उसी protocol को conform कर सकते हैं, आपको उन्हें uniformly treat करने देते हुए जहां भी वह protocol expected हो।
उदाहरण: Multiple Types Conforming to the Same Protocol
// Define a protocol `Greetable` describing required behavior
protocol Greetable {
var name: String { get }
// Define the function `greet` with no parameters and returning a String
func greet() -> String
}
// Define a struct `Human`
struct Human: Greetable {
var name: String
// Define the function `greet` with no parameters and returning a `String { "Hi, I am \(name)" }`
func greet() -> String { "Hi, I am \(name)" }
}
// Define a struct `Robot`
struct Robot: Greetable {
var name: String
// Define the function `greet` with no parameters and returning a `String { "Beep, I am \(name)" }`
func greet() -> String { "Beep, I am \(name)" }
}
// Declare a constant `greeters` (a `[Greetable]`), set to `[Human(name: "Ana"), Robot(name: "R2")]`
let greeters: [Greetable] = [Human(name: "Ana"), Robot(name: "R2")]
// Loop over `greeters`, binding each item to `greeter`
for greeter in greeters {
// Print `greeter.greet()` to the console
print(greeter.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 (बिना implementation वाला एक contract) को एक class (असली implementation वाला एक concrete type) के साथ confuse करना; एक protocol खुद instantiate नहीं हो सकता।
- यह भूल जाना कि किसी protocol को conform करने के लिए एक type को इसमें listed हर requirement implement करना चाहिए -- partial conformance एक compile error है।
- यह मान लेना कि protocols सिर्फ classes अपना सकती हैं; structs और enums भी protocols को conform कर सकते हैं।
चैप्टर सारांश
- एक
protocolmethod और property requirements का एक set define करता है इन्हें implement किए बिना। - कोई भी struct, class, या enum इसके सभी requirements implement करके एक protocol conform कर सकता है।
- Conformance type name के बाद
: ProtocolNameसे declared होता है। - Protocols class inheritance की ज़रूरत के बिना polymorphism enable करते हैं।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: