Skip to content
The Chat Archive
qwen3-235b-a22b-no-thinking· Alibaba (Qwen) LMArena Human Preference 140K

in swift i need to declare the same protocol but with two different war values like…

  1. User

    in swift i need to declare the same protocol but with two different war values like

    protocol LocalizableDisplayable: RawRepresentable where RawValue == Int { init?(rawValue: Int)

    var localizedTitle: String { get }
    var localizedDescription: String { get }
    

    }

    protocol LocalizableDisplayable: RawRepresentable where RawValue == String { init?(rawValue: String)

    var localizedTitle: String { get }
    var localizedDescription: String { get }
    

    }

    how i can do it ?

  2. Assistant

    In Swift, you cannot declare two protocols with the same name, even if they have different constraints. So this approach:

    protocol LocalizableDisplayable: RawRepresentable where RawValue == Int { ... }
    
    protocol LocalizableDisplayable: RawRepresentable where RawValue == String { ... }
    

    ...will result in a compiler error due to duplicate declaration of LocalizableDisplayable.


    ✅ Solution: Use Generics or Two Separate Protocols

    There are several ways to handle this depending on your use case.


    ✅ Option 1: Define Two Different Protocols (Recommended for Clarity)

    protocol LocalizableIntDisplayable: RawRepresentable where RawValue == Int {
        init?(rawValue: Int)
        var localizedTitle: String { get }
        var localizedDescription: String { get }
    }
    
    protocol LocalizableStringDisplayable: RawRepresentable where RawValue == String {
        init?(rawValue: String)
        var localizedTitle: String { get }
        var localizedDescription: String { get }
    }
    

    Then conform types accordingly:

    enum MyEnum: Int, LocalizableIntDisplayable {
        case foo
        case bar
    
        var localizedTitle: String {
            switch self {
            case .foo: return "Foo"
            case .bar: return "Bar"
            }
        }
    
        var localizedDescription: String {
            // ...
        }
    }
    

    ✅ Option 2: Make One Generic Protocol

    You can define a generic protocol using a type parameter or associated type:

    protocol LocalizableDisplayable: RawRepresentable {
        associatedtype RawValue
    
        init?(rawValue: RawValue)
        var localizedTitle: String { get }
        var localizedDescription: String { get }
    }
    

    This removes the need to define two separate protocols — any conforming type just needs to specify its RawValue.

    Example usage:

    enum MyEnum: String, LocalizableDisplayable {
        case login
        case logout
    
        init?(rawValue: String) {
            self.init(rawValue: rawValue)
        }
    
        var localizedTitle: String {
            return NSLocalizedString(rawValue, comment: "")
        }
    
        var localizedDescription: String {
            return NSLocalizedString(rawValue + "_description", comment: "")
        }
    }
    

    Note: This may cause issues with type inference and protocol usage unless you're careful with constraints.


    ✅ Option 3: Abstract Localization Logic Separately

    If both enums behave similarly regardless of their raw value type, consider abstracting the localization logic outside the enum.

    For example:

    protocol Localizable {
        var localizedTitle: String { get }
        var localizedDescription: String { get }
    }
    

    And provide helper functions that work with RawRepresentable separately.


    Summary

    ApproachWhen to Use
    ✅ Two distinct protocols (LocalizableIntDisplayable, LocalizableStringDisplayable)Clear separation, easy to understand
    ✅ Generic protocol (associatedtype RawValue)More flexible, less duplication
    ❌ Duplicate protocol namesNot allowed in Swift

    Let me know what kind of types you're working with (enums? structs?), and I can tailor the solution more precisely.

  3. User

    this not works init?(rawValue: String) { self.init(rawValue: rawValue) } Function call causes an infinite recursion

  4. Assistant

    You're absolutely right — the following code causes infinite recursion:

    init?(rawValue: String) {
        self.init(rawValue: rawValue)
    }
    

    ❗ Why it doesn't work

    This happens because you're calling self.init(rawValue:) inside the initializer, which is the same initializer, leading to an infinite loop.

    This pattern only works if the initializer calls a different implementation (like a concrete one provided by enum types), but when you write this in a protocol extension or conforming type without underlying support, it crashes.


    ✅ Correct Way to Conform to RawRepresentable

    When you conform a type to RawRepresentable, Swift expects that your enum (or struct) already has a built-in init?(rawValue:) initializer. You don't need to re-implement it manually unless you're doing something custom.


    ✅ Example with String Raw Value

    If you're using an enum with a raw value type like String, Swift automatically provides init?(rawValue:).

    So this works out of the box:

    enum MyEnum: String, LocalizableDisplayable {
        case login
        case logout
    
        var localizedTitle: String {
            return NSLocalizedString(rawValue, comment: "")
        }
    
        var localizedDescription: String {
            return NSLocalizedString("\(rawValue)_description", comment: "")
        }
    }
    

    No need to implement init?(rawValue:) yourself!


    ✅ If You Need Custom Initialization Logic

    Only implement init?(rawValue:) if you want to customize how the raw value maps to cases — for example, when dealing with legacy values or typos:

    enum MyEnum: String, LocalizableDisplayable {
        case login
        case logout
    
        init?(rawValue: String) {
            switch rawValue {
            case "login": self = .login
            case "logout", "signout": self = .logout // handle typo/legacy values
            default: return nil
            }
        }
    
        var localizedTitle: String {
            return NSLocalizedString(rawValue, comment: "")
        }
    
        var localizedDescription: String {
            return NSLocalizedString("\(rawValue)_description", comment: "")
        }
    }
    

    ✅ Summary

    • ❌ Don’t call self.init(rawValue:) inside init?(rawValue:) — it will crash with infinite recursion.
    • ✅ Use Swift’s automatic conformance for enums with raw values (String, Int, etc.).
    • ✅ Only override init?(rawValue:) if you need custom logic.

    Let me know if you're working with Int, String, or even custom raw types, and I can tailor the solution further!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.