# How to guarantee reference being unowned?

**URL:** <https://forums.kodeco.com/t/how-to-guarantee-reference-being-unowned/177467>\
**Category:** Swift Apprentice\
**Created:** [August 28, 2022, 12:50pm UTC](https://forums.kodeco.com/t/how-to-guarantee-reference-being-unowned/177467 "2022-08-28T12:50:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![exchessplayerh](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/exchessplayerh/32/110937_2.png) [@exchessplayerh](https://forums.kodeco.com/u/exchessplayerh)\
**Post date:** [August 28, 2022, 12:50pm UTC](https://forums.kodeco.com/t/how-to-guarantee-reference-being-unowned/177467/1 "2022-08-28T12:50:51Z")

</div>

Besides using unowned instead of weak, what can be done in order to guarantee that the reference passed to the constructor is not created during constructor invocation?

Namely, if the class has a weak reference/unowned reference member - We wouldn’t want to allows things like

```auto
class MyClass {
    unowned let someMember: MemberClass
    ...
   init(someMemeber: MemberClass) {
       self.someMember = someMember
   }
}

let classInstance = MyClass(someMember: MemberClass(...) )

```

In this case - expect a nice dangling reference error - (unowned references are particularly bad - weak references at least check for null - but we still have same problem - there is no guarantee that the class doesn’t actually own this reference).

What is the proper way to enforce it? On one hand, seems that inout parameters in the constructor solve this… On the other hand - we can not pass parameters declared as “let” this way…  
An addition to the tutorial describing how to handle this would be greatly appreciated

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [February 11, 2023, 4:51am UTC](https://forums.kodeco.com/t/how-to-guarantee-reference-being-unowned/177467/2 "2023-02-11T04:51:03Z")

</div>

This topic was automatically closed after 166 days. New replies are no longer allowed.
