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

Protocol Conformance क्या है

Conformance एक type का असल में अपना वादा निभाना है, protocol की checklist के हर item के लिए असली code प्रदान करके।
Syntax
swift
protocol ProtocolName {
    var property: Type { get }
    func method()
}

struct TypeName: ProtocolName { /* implement every requirement */ }

extension ExistingType: ProtocolName { /* add conformance */ }

Implementing All Requirements

किसी protocol को conform करने के लिए, एक type को हर property और method के लिए एक असली implementation प्रदान करना चाहिए जो protocol required के रूप में list करता है।

उदाहरण: Implementing All Requirements

markup
// Define a protocol `Describable` describing required behavior
protocol Describable {
    var description: String { get }
    // Define the function `summary` with no parameters and returning a String
    func summary() -> String
}
// Define a struct `Book`
struct Book: Describable {
    var title: String
    var description: String {
        // Return `"Book titled \(title)"` from this function
        return "Book titled \(title)"
    }
    // Define the function `summary` with no parameters and returning a String
    func summary() -> String {
        // Return `"Summary: \(description)"` from this function
        return "Summary: \(description)"
    }
}
// Declare a constant `book`, set to `Book(title: "Swift Basics")`
let book = Book(title: "Swift Basics")
// Print `book.summary()` to the console
print(book.summary())

Conforming via an Extension

Protocol conformance को एक extension के ज़रिए एक मौजूदा type में जोड़ा जा सकता है, conformance code को original type definition से अलग रखते हुए।

उदाहरण: Conforming via an Extension

markup
// Define a protocol `Named` describing required behavior
protocol Named {
    var displayName: String { get }
}
// Define a struct `Product`
struct Product {
    var title: String
}
// Extend `Product` with additional functionality
extension Product: Named {
    var displayName: String {
        // Return `"Product: \(title)"` from this function
        return "Product: \(title)"
    }
}
// Declare a constant `product`, set to `Product(title: "Headphones")`
let product = Product(title: "Headphones")
// Print `product.displayName` to the console
print(product.displayName)
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. किसी property requirement की { get } बनाम { get set } distinction भूल जाना -- { get set } conforming type की property को mutable होने की मांग करता है।
  2. Conformance declare करना (: ProtocolName) लेकिन असल में required methods में से किसी एक को implement करना भूल जाना, एक compile error का कारण बनते हुए।
  3. किसी type को एक protocol conform करने के लिए एक extension उपयोग करने की कोशिश करना जबकि वहां नई stored properties भी जोड़ना; extensions stored properties नहीं जोड़ सकते।
चैप्टर सारांश
  • किसी type को conform करने के लिए किसी protocol की मांग की हर property और method implement करनी चाहिए।
  • किसी protocol में { get } का मतलब है read-only access ज़रूरी है; { get set } एक settable property भी मांगता है।
  • Conformance type पर सीधे declared किया जा सकता है या बाद में एक extension से जोड़ा जा सकता है।
  • Compiler compile time पर conformance जांचता है, इसलिए missing requirements तुरंत पकड़े जाते हैं।
🔒

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.