不可变的值作为inout参数 [英] Immutable value as inout argument

查看:88
本文介绍了不可变的值作为inout参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个指针作为类的参数.但是,当我尝试对init进行编码时,出现以下错误:Cannot pass immutable value of type 'AnyObject?' as inout argument

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
    }
}

我想创建一些MyClass实例,它们都可以引用相同的值".然后,当我在此类中编辑此值时,它将在其他地方更改.

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.

这是我第一次在Swift中使用指针.我想我做错了...

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

推荐答案

对于具有cannot pass immutable value as inout argument错误的用户.首先检查您的参数不是可选的. Inout类型似乎不喜欢可选值.

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.

这篇关于不可变的值作为inout参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆