Why doesn't this code work?
为什么这段代码不起作用?
protocol ExampleProtocol {
var simpleDescription: String { get }
mutating func adjust()
}
extension Int: ExampleProtocol {
var simpleDescription: String {
return "The number \(self)"
}
mutating func adjust() {
self += 42
}
}
var x:Int = 7
let y:Int = x.adjust()
here is what I get on XCODE
这是我在XCODE上得到的
is there a way to make adjust() return Int without changing its definition in the protocol?
有没有办法让adjust()返回Int而不改变协议中的定义?
3 个解决方案
#1
5
Yes, you can give adjust a return value. Define it to return an Int in the protocol and class, then have it return itself in the mutating method:
是的,您可以给出调整返回值。定义它以在协议和类中返回Int,然后让它在变异方法中返回:
protocol ExampleProtocol {
var simpleDescription: String { get }
mutating func adjust() -> Int
}
extension Int: ExampleProtocol {
var simpleDescription: String {
return "The number \(self)"
}
mutating func adjust() -> Int {
self += 42
return self
}
}
var x:Int = 7
let y:Int = x.adjust() //49
#2
3
Because the adjust() function does not return value (it just change the value of its instance), you can achieve this by this orders:
因为adjust()函数不返回值(它只是更改其实例的值),您可以通过以下命令实现此目的:
var x:Int = 7
x.adjust() //adjust x self value
let y:Int = x //assigne x value to y
Hope this helps.
希望这可以帮助。
#3
0
Ques : is there a way to make adjust() return Int without changing its definition in the protocol?
问:有没有办法让adjust()返回Int而不改变协议中的定义?
Ans : No
答:没有
The definition/signature of a method tells us what it's going to take as input and what data type it's going to return.
方法的定义/签名告诉我们它将作为输入采用什么以及它将返回什么数据类型。
You need to return an Int from a method then it's signature should end with -> Int
. That's the syntax ! Can't change it.
您需要从方法返回一个Int,然后它的签名应以 - > Int结尾。这就是语法!无法改变它。
connor has provided the right piece of code you need.
connor提供了您需要的正确代码。
#1
5
Yes, you can give adjust a return value. Define it to return an Int in the protocol and class, then have it return itself in the mutating method:
是的,您可以给出调整返回值。定义它以在协议和类中返回Int,然后让它在变异方法中返回:
protocol ExampleProtocol {
var simpleDescription: String { get }
mutating func adjust() -> Int
}
extension Int: ExampleProtocol {
var simpleDescription: String {
return "The number \(self)"
}
mutating func adjust() -> Int {
self += 42
return self
}
}
var x:Int = 7
let y:Int = x.adjust() //49
#2
3
Because the adjust() function does not return value (it just change the value of its instance), you can achieve this by this orders:
因为adjust()函数不返回值(它只是更改其实例的值),您可以通过以下命令实现此目的:
var x:Int = 7
x.adjust() //adjust x self value
let y:Int = x //assigne x value to y
Hope this helps.
希望这可以帮助。
#3
0
Ques : is there a way to make adjust() return Int without changing its definition in the protocol?
问:有没有办法让adjust()返回Int而不改变协议中的定义?
Ans : No
答:没有
The definition/signature of a method tells us what it's going to take as input and what data type it's going to return.
方法的定义/签名告诉我们它将作为输入采用什么以及它将返回什么数据类型。
You need to return an Int from a method then it's signature should end with -> Int
. That's the syntax ! Can't change it.
您需要从方法返回一个Int,然后它的签名应以 - > Int结尾。这就是语法!无法改变它。
connor has provided the right piece of code you need.
connor提供了您需要的正确代码。