在Swift中对闭包的引用很少 [英] Weak reference to closure in Swift

查看:86
本文介绍了在Swift中对闭包的引用很少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来创建可观察的数据绑定属性。它正在开发中,所以我不确定最终实现将是什么,并且我对Swift还是很陌生。

I have the following code to create an observable property for data binding. It's in the works so I'm not sure what the final implementation is going to be and I'm still pretty new to Swift.

class Observable<T> {
    typealias Observer = T -> Void

    var value: T {
        didSet {
            for observer in self.observers {
                observer?(self.value)
            }
        }
    }

    var observers: [Observer?] = []

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

我想保留对Observer闭包的弱引用。我不想依靠客户通过捕获列表来确保闭包是弱的/无主的,然后再将其传递给客户。特别是因为在给定的类上可以有许多可观察的属性。

I would like to keep weak references to the Observer closures. I don't want to rely on the client to ensure that the closure is weak/unowned before passing it in, via the capture list. Especially because there can be many observable properties on a given class.

是否可以在我的Observable类中使闭包引用变弱?

Is it possible to make the closure references weak in my Observable class?

更新:

我发现了一些资源,可以帮助我实现自己想要的目标:

I found a couple of resources that I think will help me accomplish what I want:

在Swift中的方法中变得虚弱

func methodPointer<T: AnyObject>(obj: T, method: (T) -> () -> Void) -> (() -> Void) {
  return { [unowned obj] in method(obj)() }
}

以下链接引用了上面的stackoverflow答案,并提供了更多详细信息:

The following link refers to the above stackoverflow answer and goes into some more detail:

> http://blog.xebia.com/2014/10/09 / function-references-swift-and-retain-cycles /

这是一个双向绑定示例:

and this is a two-way binding example:

http://five.agency/快速解决绑定问题/

具体地说,

class BondBox<T> {
  weak var bond: Bond<T>?
  init(_ b: Bond<T>) { bond = b }
}

其中,侦听器包装在名为Bond的类中,该类在BondBox中被弱引用。

where the listener is wrapped in a class called Bond, which is weakly referenced in the BondBox.

推荐答案


是否有可能使Observable类中的闭包引用弱

Is it possible to make the closure references weak in my Observable class

否。在Swift中,只能通过弱引用来引用类实例,而函数不是类实例。 (而且它们不仅必须是类实例,而且还必须是包装类实例的Optional。)

No. Only class instances can be referred to via weak references in Swift, and a function is not a class instance. (And not only must they be class instances, they must be an Optional wrapping a class instance.)

有一些非常明显的方法可以解决,或者当然,这是最简单的方法作为包装器类。但是我实际上不建议在这种情况下这样做,因为您没有说服我,这里首先需要弱引用函数。请记住,对没有强引用的对象的弱引用将立即失去引用,并指向nil。我无法相信那是您想要的。我认为您在这里树错了树。

There are some pretty obvious ways around this, or course - the simplest being a wrapper class. But I do not actually recommend that in this situation, because you have not convinced me that weak references to functions are needed here in the first place. Remember, a weak reference to an object to which there is no strong reference will instantly lose the reference and will be pointing at nil. I can't believe that is what you want. I think you're barking up a wrong tree here.

这篇关于在Swift中对闭包的引用很少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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