← Back to Swift Course | Chapter 9: Inheritance & Protocols | Lesson 4 of 7

Protocol के Basics

एक protocol एक checklist जैसा है उन चीज़ों की जो एक type वादा करता है कर सकता है, बिल्कुल यह कहे बिना कि यह इन्हें कैसे करता है।
Syntax
swift
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

markup
// 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())

Multiple Types Conforming to the Same Protocol

अलग-अलग, unrelated types सभी उसी protocol को conform कर सकते हैं, आपको उन्हें uniformly treat करने देते हुए जहां भी वह protocol expected हो।

उदाहरण: Multiple Types Conforming to the Same Protocol

markup
// 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())
}
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. एक protocol (बिना implementation वाला एक contract) को एक class (असली implementation वाला एक concrete type) के साथ confuse करना; एक protocol खुद instantiate नहीं हो सकता।
  2. यह भूल जाना कि किसी protocol को conform करने के लिए एक type को इसमें listed हर requirement implement करना चाहिए -- partial conformance एक compile error है।
  3. यह मान लेना कि protocols सिर्फ classes अपना सकती हैं; structs और enums भी protocols को conform कर सकते हैं।
चैप्टर सारांश
  • एक protocol method और 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:

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.