Immutable value as inout argument

Tulleb picture Tulleb · Jul 27, 2016 · Viewed 11.4k times · Source

I would like to have a pointer as a parameter of a class. But when I am trying to code the init, I am having this error: Cannot pass immutable value of type 'AnyObject?' as inout argument

class MyClass {
    var valuePointer: UnsafeMutablePointer<AnyObject?>

    init(value: inout AnyObject?) {
        self.valuePointer = &value
    }
}

I would like to create some instance of MyClass which can all refer to the same "value". Then, when I am editing this value in this class, it would change everywhere else.

This is the first time I'm working with pointer in Swift. I guess I am doing it wrong...

Answer

Tulleb picture Tulleb · Aug 1, 2016

For those who has the cannot pass immutable value as inout argument error. Check that your argument is not optional first. Inout type doesn't seems to like optional values.