Reader small image

You're reading from  Learning Swift Second Edition - Second Edition

Product typeBook
Published inMar 2016
Reading LevelBeginner
Publisher
ISBN-139781785887512
Edition2nd Edition
Languages
Tools
Right arrow
Author (1)
Andrew J Wagner
Andrew J Wagner
author image
Andrew J Wagner

Contacted on 5 Aug 16 Andrew J Wagner is a software developer who concentrates on iOS development and backend web services. He has a degree in computer engineering from Rensselaer Polytechnic Institute, New York. Currently, he works for a development shop based in Denver, CO named Chronos Interactive. He has experience working with and for large-scale companies and small-scale companies as well as running his own contracting and app companies. He is passionate about using computers as a creative outlet and writing software that is beautiful in implementation, functionality, and experience. When he isn't working or spending time with friends and family, he writes for his blog at http://drewag.me. I would like to thank my friends and family for being there for me as support for both my troubles and triumphs. Without their encouragement, I would not have finished this book or achieved any of the other things in my life that make me proud. An especially big thanks to my parents, Fern and Joe, for continually providing me the the tools I need to do the things I love.
Read more about Andrew J Wagner

Right arrow

Lost objects


It is a great idea to always keep strong reference cycles in mind, but if we are too aggressive with the use of weak and unowned references, we can run into the opposite problem, where an object is deleted before we intended it to be.

Between objects

With an object this will happen if all of the references to the object are weak or unowned. This won't be a fatal mistake if we use weak references, but if this happens with an unowned reference it will crash your program.

For example, let's look at the preceding example with an extra weak reference:

class SteeringWheel {
    weak var car: Car?
}
class Car {
    weak var steeringWheel: SteeringWheel!

    init(steeringWheel: SteeringWheel) {
        self.steeringWheel = steeringWheel
        steeringWheel.car = self
    }
}

let wheel = SteeringWheel()
let car = Car(steeringWheel: wheel)

This code is the same as the preceding one except that both the car property of SteeringWheel and the steeringWheel property of Car are weak. This means...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Learning Swift Second Edition - Second Edition
Published in: Mar 2016Publisher: ISBN-13: 9781785887512

Author (1)

author image
Andrew J Wagner

Contacted on 5 Aug 16 Andrew J Wagner is a software developer who concentrates on iOS development and backend web services. He has a degree in computer engineering from Rensselaer Polytechnic Institute, New York. Currently, he works for a development shop based in Denver, CO named Chronos Interactive. He has experience working with and for large-scale companies and small-scale companies as well as running his own contracting and app companies. He is passionate about using computers as a creative outlet and writing software that is beautiful in implementation, functionality, and experience. When he isn't working or spending time with friends and family, he writes for his blog at http://drewag.me. I would like to thank my friends and family for being there for me as support for both my troubles and triumphs. Without their encouragement, I would not have finished this book or achieved any of the other things in my life that make me proud. An especially big thanks to my parents, Fern and Joe, for continually providing me the the tools I need to do the things I love.
Read more about Andrew J Wagner