Cannot assign to property in protocol - Swift compiler error

后端 未结 3 657
我在风中等你
我在风中等你 2020-12-01 07:10

I\'m banging my head against the wall with the following code in Swift. I\'ve defined a simple protocol:

protocol Nameable {
    var name : String { get set          


        
相关标签:
3条回答
  • 2020-12-01 08:00

    It's because, Nameable being a protocol, Swift doesn't know what kind (flavor) of object your function's incoming Nameable is. It might be a class instance, sure - but it might be a struct instance. And you can't assign to a property of a constant struct, as the following example demonstrates:

    struct NameableStruct : Nameable {
        var name : String = ""
    }
    let ns = NameableStruct(name:"one")
    ns.name = "two" // can't assign
    

    Well, by default, an incoming function parameter is a constant - it is exactly as if you had said let in your function declaration before you said nameable.

    The solution is to make this parameter not be a constant:

    func nameNameable(var nameable: Nameable, name: String ) {
                      ^^^
    

    NOTE Later versions of Swift have abolished the var function parameter notation, so you'd accomplish the same thing by assigning the constant to a variable:

    protocol Nameable {
        var name : String { get set }
    }
    func nameNameable(nameable: Nameable, name: String) {
        var nameable = nameable // can't compile without this line
        nameable.name = name
    }
    
    0 讨论(0)
  • 2020-12-01 08:02

    @matt's anwer is correct.

    Another solution is to declare Nameable as a class only protocol.

    protocol Nameable: class {
    //               ^^^^^^^ 
        var name : String { get set }
    }
    

    I think, this solution is more suitable for this case. Because nameNameable is useless unless nameable is a instance of class.

    0 讨论(0)
  • 2020-12-01 08:05

    Here, i written some code, that might give some idea on Associated generic type Usage:

    protocol NumaricType 
    {
       typealias elementType
       func plus(lhs : elementType, _ rhs : elementType) -> elementType
       func minus(lhs : elementType, _ rhs : elementType) -> elementType
    }
    
    struct Arthamatic :NumaricType {
    
    func addMethod(element1 :Int, element2 :Int) -> Int {
       return plus(element1, element2)
    }
    func minusMethod(ele1 :Int, ele2 :Int) -> Int {
        return minus(ele1, ele2)
    }
    typealias elementType = Int
    
    func plus(lhs: elementType,  _ rhs: elementType) -> elementType {
        return lhs + rhs
    }
    func minus(lhs: elementType, _ rhs: elementType) -> elementType {
        return lhs - rhs
     }
    }
     **Output:**
    let obj =  Arthamatic().addMethod(34, element2: 45) // 79
    
    0 讨论(0)
提交回复
热议问题