Ios 当一个类符合包含变异函数的协议时会发生什么?

Ios 当一个类符合包含变异函数的协议时会发生什么?,ios,swift,protocols,Ios,Swift,Protocols,我只是在试验Swift中的协议编程。在此期间,我遇到了以下场景。 我们有一个协议,比如 protocol SomeProtocol { ..... mutating func mutatingFunc() ..... } struct MyStruct { var width = 0, height = 0 } extension MyStruct: SomeProtocol { //1. If I remove the mutating keyword f

我只是在试验Swift中的协议编程。在此期间,我遇到了以下场景。 我们有一个协议,比如

protocol SomeProtocol {
    .....
    mutating func mutatingFunc()
    .....
}
struct MyStruct {
    var width = 0, height = 0
}

extension MyStruct: SomeProtocol {

//1. If I remove the mutating keyword from following method definition, compiler will give me error, that's understandable as we are modifying structure members
//2. Also note that, if I remove the mutating keyword while defining protocol & keep mutating keyword in the below method, the compiler will say that structure doesn't conform to protocol

    mutating func mutatingMethod() {
        width += 10
        height += 10
    }

}

class MyClass {
    var width = 10
}

extension MyClass: SomeProtocol {

//1. However while implementing the protocol method in class, I can implement the same protocol method, and the compiler doesn't complaint even if I don't mention mutating keyword. 
//2. But compiler will complain that mutating isn't valid on methods in classes or class-bound protocols, if I mention the mutating keyword
    func mutatingMethod() {
        width += 10
        print(width)
    }
}
protocol AnotherProtocol {
    func nonMutatingFunc()
}

struct MyAnotherStruct {
    var width = 0
}
extension MyAnotherStruct: SomeProtocol, AnotherProtocol {
    func mutatingFunc() {
//1. Compiler is happy even without the mutating keyword as we are not modifying structure members, also the structure conforms to SomeProtocol.
        print("Hello World")
    }
    mutating func nonMutatingFunc() {
    //1. Compiler cries that we are not conforming to AnotherProtocol as the function is mutating
        width += 10
    }

}
现在让我们假设我有一个名为MyClass的类,它符合我的SomeProtocol,比如

protocol SomeProtocol {
    .....
    mutating func mutatingFunc()
    .....
}
struct MyStruct {
    var width = 0, height = 0
}

extension MyStruct: SomeProtocol {

//1. If I remove the mutating keyword from following method definition, compiler will give me error, that's understandable as we are modifying structure members
//2. Also note that, if I remove the mutating keyword while defining protocol & keep mutating keyword in the below method, the compiler will say that structure doesn't conform to protocol

    mutating func mutatingMethod() {
        width += 10
        height += 10
    }

}

class MyClass {
    var width = 10
}

extension MyClass: SomeProtocol {

//1. However while implementing the protocol method in class, I can implement the same protocol method, and the compiler doesn't complaint even if I don't mention mutating keyword. 
//2. But compiler will complain that mutating isn't valid on methods in classes or class-bound protocols, if I mention the mutating keyword
    func mutatingMethod() {
        width += 10
        print(width)
    }
}
protocol AnotherProtocol {
    func nonMutatingFunc()
}

struct MyAnotherStruct {
    var width = 0
}
extension MyAnotherStruct: SomeProtocol, AnotherProtocol {
    func mutatingFunc() {
//1. Compiler is happy even without the mutating keyword as we are not modifying structure members, also the structure conforms to SomeProtocol.
        print("Hello World")
    }
    mutating func nonMutatingFunc() {
    //1. Compiler cries that we are not conforming to AnotherProtocol as the function is mutating
        width += 10
    }

}
假设我有另一个结构,另一个协议

protocol SomeProtocol {
    .....
    mutating func mutatingFunc()
    .....
}
struct MyStruct {
    var width = 0, height = 0
}

extension MyStruct: SomeProtocol {

//1. If I remove the mutating keyword from following method definition, compiler will give me error, that's understandable as we are modifying structure members
//2. Also note that, if I remove the mutating keyword while defining protocol & keep mutating keyword in the below method, the compiler will say that structure doesn't conform to protocol

    mutating func mutatingMethod() {
        width += 10
        height += 10
    }

}

class MyClass {
    var width = 10
}

extension MyClass: SomeProtocol {

//1. However while implementing the protocol method in class, I can implement the same protocol method, and the compiler doesn't complaint even if I don't mention mutating keyword. 
//2. But compiler will complain that mutating isn't valid on methods in classes or class-bound protocols, if I mention the mutating keyword
    func mutatingMethod() {
        width += 10
        print(width)
    }
}
protocol AnotherProtocol {
    func nonMutatingFunc()
}

struct MyAnotherStruct {
    var width = 0
}
extension MyAnotherStruct: SomeProtocol, AnotherProtocol {
    func mutatingFunc() {
//1. Compiler is happy even without the mutating keyword as we are not modifying structure members, also the structure conforms to SomeProtocol.
        print("Hello World")
    }
    mutating func nonMutatingFunc() {
    //1. Compiler cries that we are not conforming to AnotherProtocol as the function is mutating
        width += 10
    }

}
所以现在我的观察是,

  • 对于一个类,无论函数是否在协议中被称为变异,我们都不允许将类中的方法称为变异
  • 对于一个结构,如果实现的方法不修改成员,我们就不必提及func,因为它在协议中是变异的,甚至很难
  • 对于结构,如果我们将协议方法实现为mutating func,并且在协议中未将该方法指定为mutating func。编译器给出错误
  • 目前,由于上述场景,我对编译器处理结构的方式感到困惑。如果有人能解释协议内函数变异的重要性,即“当我们将协议内的函数声明为变异时,我们是否应该在实现它时使函数变异?”这将是非常棒和有用的

    另外,我知道什么是变异函数,我只是对协议中定义的变异方法感到困惑

    提前谢谢。

    我不明白什么是“困惑”。你很好地阐明了规则!结构可以将协议的
    变异
    函数实现为非变异,但不能将协议的非变异函数实现为
    变异
    。一旦你说了这些规则,就没有什么可“混淆”的了。规则就是规则


    类没有变异函数,所以你的研究有点不相关。如果您将协议声明为
    协议,则协议中的
    变异
    注释将是非法的。

    我的在线书籍()也会告诉您同样的事情:“如果协议缺少
    变异
    ,则采纳者无法添加该变异。但是,如果协议中有变异,采纳者可能会忽略它。“我的问题是,当你将协议中的func声明为变异时,我们是否应该在实现它时使func变异?谢谢你的解释。行-“如果协议中没有变异,采纳者不能添加变异。但是,如果协议中有变异,采用者可能会忽略它。“消除了我的困惑。谢谢,谢谢你的接受/同意投票,但相信我,你已经很好地解决了这个问题!你的侦查工作非常出色。你准确地表达了事实。真的没有更多需要知道的了。谢谢马特。我不知道——”如果协议缺少变异,则采纳者不能添加变异。但是,如果协议中有变异,采用者可能会忽略它。“我在任何文档中都没有遇到过。这就是为什么我感到困惑。非常感谢您的回答:)