unowned-references

Swift Weak Reference Much Slower than Strong Reference

淺唱寂寞╮ 提交于 2020-07-03 03:43:31
问题 I'm building a physics engine in Swift. After making some recent additions to the engine and running the benchmarking tests I noticed the performance was drastically slower. For example, in the screenshots below you can see how the FPS dropped from 60 to 3 FPS (FPS is in the bottom-right corner). Eventually, I traced the problem down to just a single line of code: final class Shape { ... weak var body: Body! // This guy ... } At some point in my additions I added a weak reference from the

In Swift, unowned vs. weak reference [duplicate]

别说谁变了你拦得住时间么 提交于 2019-12-18 12:45:54
问题 This question already has answers here : What is the difference between a weak reference and an unowned reference? (7 answers) Closed 3 years ago . If you have a Dog with a weak reference to Bone , that means that the Dog is the 'owner' of the reference in this situation, and it USES bone, but bone can go out of existence and Dog can still function (because the reference to bone is an optional). However with 'unowned', it seems to be that the keyword 'unowned' is used not in the owner's

Do capture lists of inner closures need to redeclare `self` as `weak` or `unowned`?

一笑奈何 提交于 2019-12-18 02:43:16
问题 If I have a closure passed to a function like this: someFunctionWithTrailingClosure { [weak self] in anotherFunctionWithTrailingClosure { [weak self] in self?.doSomething() } } If I declare self as [weak self] in someFunctionWithTrailingClosure 's capture list without redeclaring it as weak again in the capture list of anotherFunctionWithTrailingClosure self is already becoming an Optional type but is it also becoming a weak reference as well? Thanks! 回答1: The [weak self] in

Do capture lists of inner closures need to redeclare `self` as `weak` or `unowned`?

本小妞迷上赌 提交于 2019-12-18 02:43:06
问题 If I have a closure passed to a function like this: someFunctionWithTrailingClosure { [weak self] in anotherFunctionWithTrailingClosure { [weak self] in self?.doSomething() } } If I declare self as [weak self] in someFunctionWithTrailingClosure 's capture list without redeclaring it as weak again in the capture list of anotherFunctionWithTrailingClosure self is already becoming an Optional type but is it also becoming a weak reference as well? Thanks! 回答1: The [weak self] in

Add [unowned self] to the closure argument Swift

孤者浪人 提交于 2019-12-11 01:18:45
问题 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 {

Do capture lists of inner closures need to redeclare `self` as `weak` or `unowned`?

喜夏-厌秋 提交于 2019-11-29 02:07:41
If I have a closure passed to a function like this: someFunctionWithTrailingClosure { [weak self] in anotherFunctionWithTrailingClosure { [weak self] in self?.doSomething() } } If I declare self as [weak self] in someFunctionWithTrailingClosure 's capture list without redeclaring it as weak again in the capture list of anotherFunctionWithTrailingClosure self is already becoming an Optional type but is it also becoming a weak reference as well? Thanks! The [weak self] in anotherFunctionWithTrailingClosure is not needed. You can empirically test this: class Experiment { func