Ios 当一个类符合包含变异函数的协议时会发生什么?
我只是在试验Swift中的协议编程。在此期间,我遇到了以下场景。 我们有一个协议,比如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
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声明为变异时,我们是否应该在实现它时使func变异?谢谢你的解释。行-“如果协议中没有变异,采纳者不能添加变异。但是,如果协议中有变异,采用者可能会忽略它。“消除了我的困惑。谢谢,谢谢你的接受/同意投票,但相信我,你已经很好地解决了这个问题!你的侦查工作非常出色。你准确地表达了事实。真的没有更多需要知道的了。谢谢马特。我不知道——”如果协议缺少变异,则采纳者不能添加变异。但是,如果协议中有变异,采用者可能会忽略它。“我在任何文档中都没有遇到过。这就是为什么我感到困惑。非常感谢您的回答:)