如何通过引用传递结构? [英] How to pass structure by reference?

查看:46
本文介绍了如何通过引用传递结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一些现有的结构,但我想使用引用"行为,我该如何实现?

If I have some existing struct, but I want to use "Reference" behavior, how do I achieve that?

我可以编写一些简单的类持有者

I can write some simple class-holder like

class Box<T> {
    var value: T
    init(_ value: T) {
        self.value = value
    }
}

我猜标准库里一定有ready class,但是没找到.

I guess there must be ready class in the standard library, but I didn't find it.

我想将该引用存储在我的类中,因此 inout 参数不是我需要的.

I want to store that reference in my class, so inout parameter isn't what I need.

推荐答案

对我来说最好的变体是使用 class-holder:

For me the best variant was using class-holder:

class Ref<T> {
  var value: T

  init(_ value: T) {
    self.value = value
  }
}

这篇关于如何通过引用传递结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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