标签: unowned-references

捕获内部闭包列表是否需要将"self"重新声明为"weak"或"unowned"?

如果我有一个闭包传递给这样的函数:

 someFunctionWithTrailingClosure { [weak self] in
     anotherFunctionWithTrailingClosure { [weak self] in 
         self?.doSomething()
     }
 }
Run Code Online (Sandbox Code Playgroud)

如果我[weak self]someFunctionWithTrailingClosure自己的捕获列表中声明自己没有重新声明它weak再次在捕获列表中anotherFunctionWithTrailingClosure self已经成为一种Optional类型但是它也成为一个weak参考?

谢谢!

closures weak-references automatic-ref-counting swift unowned-references

11
推荐指数
2
解决办法
1168
查看次数

迅速.unowned只能应用于类和类绑定协议类型.弱者工作得很好

请将问题读到最后,因为它似乎是许多类似其他人的副本,但事实并非如此.大多数其他问题使用带let关键字的闭包来捕获对象init之前的弱或无主自我.我不.

我的代码:

class Singleton : ObserverProtocol {

    static let shared = Singleton()

    private let obs : Observer = Observer.init()

    private init() { self.obs.responder = self }

    func observe(_ object : Any) {}

    fileprivate class Observer : NSObject {
        unowned var responder : ObserverProtocol!
        func observe(_ obj : Any) {
            self.responder.observe(obj)
        }
    }
}

fileprivate protocol ObserverProtocol : class {
    func observe(_ object : Any)
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译时,我会突出显示错误 unowned var responder : ObserverProtocol!

'unowned'可能只适用于类和类绑定协议类型,而不是'ObserverProtocol!'

如果我unowned改为weak我可以编译.

显然有一些 …

swift swift-protocols unowned-references

8
推荐指数
1
解决办法
4261
查看次数

Swift 弱引用比强引用慢得多

我正在用 Swift 构建一个物理引擎。在对引擎进行了一些最近的添加并运行了基准测试之后,我注意到性能大大降低了。例如,在下面的屏幕截图中,您可以看到 FPS 如何从 60 FPS 下降到 3 FPS(FPS 位于右下角)。最终,我将问题归结为一行代码:

final class Shape {
    ...
    weak var body: Body! // This guy
    ...
}
Run Code Online (Sandbox Code Playgroud)

在我添加的某个时候,我添加了一个从Shape类到Body类的弱引用。这是为了防止强引用循环,因为Body也有对Shape.

不幸的是,弱引用似乎有很大的开销(我想将它清零的额外步骤)。我决定通过构建下面物理引擎的大规模简化版本并对不同的参考类型进行基准测试来进一步研究这一点。


import Foundation

final class Body {
    let shape: Shape
    var position = CGPoint()
    init(shape: Shape) {
        self.shape = shape
        shape.body = self
        
    }
}

final class Shape {
    weak var body: Body! //****** This line is the problem ******
    var vertices: [CGPoint] = []
    init() …
Run Code Online (Sandbox Code Playgroud)

performance weak-references strong-references swift unowned-references

8
推荐指数
1
解决办法
494
查看次数

关于应将[无人认领的自我]放在哪里的混乱

我有一个保留的周期,因此不会调用我的viewcontroller的deinit,并且我试图解决我添加的[unown self],但是我不太确定在我的情况下将无人管理的位置:

情况1

class YADetailiViewController: UIViewController {
 var subscription: Subscription<YAEvent>?

 override func viewDidLoad() {
    super.viewDidLoad()
    if let query = self.event.subscribeQuery() {
        self.subscription = Client.shared.subscribe(query)
        self.subscription?.handle(Event.updated) {
            query, object in
            DispatchQueue.main.async {
                [unowned self] in// Put unowned here won't break the cycle, but it does compile and run 
                self.pageViewLabel.text = String(object.pageViews) + " VIEW" + ((object.pageViews > 1) ? "S" : "")
            }
        }
    }
 }
}
Run Code Online (Sandbox Code Playgroud)

情况二

 override func viewDidLoad() {
    super.viewDidLoad()
    if let query = self.event.subscribeQuery() {
        self.subscription = …
Run Code Online (Sandbox Code Playgroud)

strong-references swift unowned-references

5
推荐指数
1
解决办法
161
查看次数

在斯威夫特,无主与弱参考

如果你的有一个弱的参考,这意味着狗在这种情况下是参考的"拥有者",它使用骨骼,但骨骼可以不存在,狗仍然可以运作(因为参考to bone是可选的).

然而,对于"无主",似乎关键字"无主"不是在所有者的引用声明中使用,而是在另一个对象中使用.例如,Bone对其狗的引用被标记为"无主".

无主是不安全的.如果所有者在程序中的某个时刻不存在,它可能会崩溃,并且它不能是可选的.为什么人们会使用无主而不是弱引用?

为什么不用弱?从我的理解来看,这只是与大声失败和失败的失败有关.在无主的情况下,如果骨头没有狗,应用程序将始终崩溃,而如果我们使用弱,您将最终得到一个仍然存在的骨骼,带有"幽灵"狗.

reference weak-references ios swift unowned-references

4
推荐指数
1
解决办法
4567
查看次数

ARC是否保存计数以备不时之需?

ARC是否保留对对象的无主引用计数?

那么,如果某个对象的强引用计数达到0并且该对象的未拥有引用计数> 0,则该对象将被取消初始化但未取消分配?并且只有当强大且无主的引用计数达到0时,它才会被取消分配吗?

我在一篇有关Medium的文章中阅读过),但是我不确定它是正确的。

automatic-ref-counting strong-references swift unowned-references

3
推荐指数
1
解决办法
344
查看次数

对于Swift单身人士,总是使用[无主自我]安全吗?

由于共享单例实例将始终存在,我们是否可以安全地使用[unowned self]该单例类中的所有闭包?

singleton memory-management reference-counting swift unowned-references

3
推荐指数
2
解决办法
876
查看次数

Add [unowned self] to the closure argument Swift

I have a function with a completion handler, returning one parameter or more.

In a client, when executing a completion handler, I'd like to have an unowned reference to self, as well as having access to the parameter passed.

Here is the Playground example illustrating the issue and the goal I'm trying to achieve.

import UIKit

struct Struct {
  func function(completion: (String) -> ()) {
    completion("Boom!")
  }

  func noArgumentsFunction(completion: () -> Void) {
    completion()
  }
}

class Class2 { …
Run Code Online (Sandbox Code Playgroud)

closures automatic-ref-counting swift unowned-references

2
推荐指数
1
解决办法
555
查看次数