Questions tagged [unowned-references]
18 questions
25
votes
3 answers
Do capture lists of inner closures need to redeclare `self` as `weak` or `unowned`?
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…

Nirma
- 5,640
- 4
- 35
- 47
10
votes
1 answer
Swift Weak Reference Much Slower than Strong Reference
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…

Epic Byte
- 33,840
- 12
- 45
- 93
9
votes
1 answer
Swift. unowned may only be applied to class and class-bound protocol types. weak works fine
Please read the question to the end as it seems to be the duplicate of many similar others but it is not. Most of the other questions use closures with let keyword to capture weak or unowned self before object init. I do not.
My code:
class…

iur
- 2,056
- 2
- 13
- 30
6
votes
1 answer
Does ARC hold a count for unowned reference?
Is it true that ARC keeps a count of unowned references to an object?
So, if the strong reference count of an object reaches 0 and the unowned reference count of that object is > 0 the object is de-initialized but not de-allocated? And only when the…

Andrew Clark
- 61
- 2
5
votes
1 answer
Confusion about where should put the [unowned self]
I have a retained cycle so my viewcontroller's deinit won't be called, and I'm trying to resolve this my adding [unowned self], but I'm not too sure where to put the unowned in my cases:
Case 1
class YADetailiViewController: UIViewController {
var…

Kesong Xie
- 1,316
- 3
- 15
- 35
5
votes
2 answers
Safe to always use [unowned self] for Swift singletons?
Since the shared singleton instance will always be around, can we safely use [unowned self] in all closures within that singleton class?

SchoonSauce
- 291
- 1
- 3
- 14
4
votes
1 answer
Understanding weak and unowned reference in Swift under the hood
I want fully understand what going inside weak and unowned referance in Swift.
For this i read MikeAsh and got some questions.
What already known:
when there is no weak (and, i suppose, unowned) object reference, the strong reference counter is…

Banannzza
- 63
- 5
4
votes
1 answer
In Swift, unowned vs. weak reference
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).…

user7024499
- 951
- 1
- 10
- 24
3
votes
1 answer
Swift - @escaping and capture list clarification
I tried to dig on this subject as much as i could, but still i've a few things that wasn't settled down in my head and i will be grateful to get clarification about them.. so i made a few questions..
How the compiler know that i've to add @escaping…

r.pul
- 109
- 2
- 9
3
votes
1 answer
Weak or Unowned or None
I have a ViewController class as shown below:
class ViewController {
var viewModel = ViewModel()
viewDidLoad() {
self.viewModel.showAlert = { [weak self] in
self?.alert()
}
}
func alert() {
//…

Savan Kavaiya
- 33
- 1
- 5
2
votes
2 answers
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…

Richard Topchii
- 7,075
- 8
- 48
- 115
1
vote
1 answer
Difference between unowned var something: Something! and weak var something: Something! in Swift
Consider the following example given below:
protocol Something: AnyObject {
func f()
}
class A {
unowned var something1: Something!
weak var something2: Something!
func f() {
something1.f()
something2.f()
…

Roman Podymov
- 4,168
- 4
- 30
- 57
1
vote
2 answers
Why weakifying a strong reference by using a local variable doesn't work?
(I understand how ARC works and the difference between weak and unowned. The question is about a specific use of them and why it doesn't work. I'll use unowned in the example below just for simplicity.)
See example below. Note line 10, which is…

rayx
- 1,329
- 10
- 23
1
vote
1 answer
Swift unowned self leaking when 'owned' by a view being presented
I am experiencing a leak with unowned self under conditions where, to the best of my knowledge, there shouldn't be a leak. Let me show an example, it is a little contrived, so bear with me, I've tried to make the simplest case I could.
Suppose I…

Darumar
- 71
- 9
1
vote
2 answers
How Unowned reference works with capture variables in Swift
There are a lot of tutorials on ARC. But I am not understanding the clear working of unowned or weak as how the reference captured variables becomes null.
Apple Document :
Define a capture in a closure as an unowned reference when the closure
and…

salman siddiqui
- 882
- 1
- 10
- 28