Hi
I'm trying to figure out the following gives me an error.
protocol SomeProtocol
{
var name: String
{
get
set
}
}
class SomeName : SomeProtocol
{
public var name: String
init()
{
self.name = ""
}
init(name: String)
{
self.name = name
}
}
class SomeContext
{
private init()
{
}
open static let shared = SomeContext()
public func someMethod(with value: inout T, age: [Int])
{
print("before: \(value.name)")
value.name = "foo"
print("after: \(value.name)")
print("age: \(age)")
}
}
func createSomeName()
{
var value = SomeName(name: "Fred")
testInout(value: &value)
print(value.name)
}
func testInout(value: inout SomeProtocol)
{
SomeContext.shared.someMethod(with: &value, age: [1,2,3])
}
createSomeName()
On line 47, I get this error:
error: cannot pass immutable value as inout argument: implicit conversion from 'SomeName' to 'SomeProtocol' requires a temporary
testInout(value: &value)
On line 53, I get this error:
error: in argument type 'inout SomeProtocol', 'SomeProtocol' does not conform to expected type 'SomeProtocol'
SomeContext.shared.someMethod(with: &value, age: [1,2,3])
Basically I need to be able to pass class/or struct to a method, which passes it to another method which can modify the value i.e name. I tried using a struct but ended up with the same errors.
If I replace line 47 with line 53, it seems to work fine with either a struct or class
Any ideas how to work around this problem?
Thanks