<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I have a situation where I have a leak that I do not understand. I would be very grateful if someone could explain it to me and offer an idea of how I can make the pattern work without leaking:<div class=""><br class=""></div><div class="">Consider two code snippets, the first of which leaks, while the second does not.</div><div class=""><br class=""></div><div class=""><div class="">Case 1: This example leaks 2 32-byte objects..</div><div class=""><br class=""></div><div class=""><font face="Consolas" class="">in FontSelectorDialog.swift:</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>override func prepare(for segue: UIStoryboardSegue, sender: Any?) {</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>switch segue.identifier! {</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>< ... ></font></div><div class=""><span class="Apple-tab-span" style="white-space:pre"><font face="Consolas" class="">        </font></span></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case "ChooseTextColor" :</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>let target = segue.destination as! ColorChooser</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>target.textChooserType = .text<span class="Apple-tab-span" style="white-space: pre;">        </span>// <===</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>< ... ></font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>}</font></div><div class=""><span class="Apple-tab-span" style="white-space:pre"><font face="Consolas" class="">                </font></span></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case "ChooseBackgroundColor" :</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>let target = segue.destination as! ColorChooser</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>target.textChooserType = .bkgnd<span class="Apple-tab-span" style="white-space: pre;">        </span>// <===</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>< ... ></font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>}</font></div><div class=""><span class="Apple-tab-span" style="white-space:pre"><font face="Consolas" class="">                </font></span></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>default : break</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div class=""><font face="Consolas" class=""><br class=""></font></div><div class=""><font face="Consolas" class="">in ColorChooser.swift:</font></div><div class=""><font face="Consolas" class=""><br class=""></font></div><div class=""><font face="Consolas" class="">internal enum ColorCat {<span class="Apple-tab-span" style="white-space:pre">                        </span>// <===</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case: .text<span class="Apple-tab-span" style="white-space:pre">                                </span>// <===</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case: .bkgnd<span class="Apple-tab-span" style="white-space:pre">                                </span>// <===</font></div><div class=""><font face="Consolas" class="">}<span class="Apple-tab-span" style="white-space:pre">                                                </span>// <===</font></div><div class=""><font face="Consolas" class=""><br class=""></font></div><div class=""><font face="Consolas" class="">internal class ColorChooser : UITableViewController {</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>internal var textChooserType : ColorCat = .text<span class="Apple-tab-span" style="white-space: pre;">        </span>// <===</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>< ... ></font></div><div class=""><font face="Consolas" class="">}</font></div><div class=""><br class=""></div><div class="">Case 2: This example does not leak...</div><div class=""><br class=""></div><div class=""><font face="Consolas" class="">in FontSelectorDialog.swift:</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>override func prepare(for segue: UIStoryboardSegue, sender: Any?) {</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>switch segue.identifier! {</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>< ...></font></div><div class=""><span class="Apple-tab-span" style="white-space:pre"><font face="Consolas" class="">                </font></span></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case "ChooseTextColor" :</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>let target = segue.destination as! ColorChooser</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>target.textChooserType = true<span class="Apple-tab-span" style="white-space:pre">        </span>// <===</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>< ... ></font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>}</font></div><div class=""><span class="Apple-tab-span" style="white-space:pre"><font face="Consolas" class="">                </font></span></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case "ChooseBackgroundColor" :</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>let target = segue.destination as! ColorChooser</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>target.textChooserType = false<span class="Apple-tab-span" style="white-space:pre">        </span>// <===</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>< ... ></font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>}</font></div><div class=""><span class="Apple-tab-span" style="white-space:pre"><font face="Consolas" class="">                </font></span></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>default : break</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div class=""><font face="Consolas" class=""><br class=""></font></div><div class=""><font face="Consolas" class="">in ColorChooser.swift:</font></div><div class=""><font face="Consolas" class=""><br class=""></font></div><div class=""><font face="Consolas" class="">internal class ColorChooser : UITableViewController {</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>internal var textChooserType : Bool = true<span class="Apple-tab-span" style="white-space:pre">        </span>// <===</font></div><div class=""><font face="Consolas" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>< ... ></font></div><div class=""><font face="Consolas" class="">}</font></div></div><div class=""><br class=""></div><div class="">Can someone explain why? And how can I implement this using an enum. I want to use an enum here because:</div><div class="">(a) it is easier to read and understand and seems more “swiftly”</div><div class="">(b) Someday, I may want to add additional options, which would be easy with an enum but difficult using the current (Bool) implementation.</div><div class=""><br class=""><div class="">
<div style="orphans: auto; text-align: start; text-indent: 0px; widows: auto; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="orphans: auto; text-align: start; text-indent: 0px; widows: auto; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="orphans: auto; text-align: start; text-indent: 0px; widows: auto; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="orphans: auto; text-align: start; text-indent: 0px; widows: auto; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="orphans: auto; text-align: start; text-indent: 0px; widows: auto; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="orphans: auto; text-align: start; text-indent: 0px; widows: auto; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="orphans: auto; text-align: start; text-indent: 0px; widows: auto; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="orphans: 2; text-align: -webkit-auto; text-indent: 0px; widows: 2;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="orphans: 2; text-indent: 0px; widows: 2;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="orphans: 2; text-indent: 0px; widows: 2;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="orphans: 2; text-indent: 0px; widows: 2;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; orphans: 2; text-indent: 0px; widows: 2; border-spacing: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; orphans: 2; text-indent: 0px; widows: 2; border-spacing: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; orphans: 2; text-indent: 0px; widows: 2; border-spacing: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; orphans: 2; text-indent: 0px; widows: 2; border-spacing: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; orphans: 2; text-indent: 0px; widows: 2; border-spacing: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; orphans: 2; text-indent: 0px; widows: 2; border-spacing: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div style="color: rgb(0, 0, 0); font-family: Arial; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px;" class="">Cheers,</div><div style="color: rgb(0, 0, 0); font-family: Arial; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px;" class=""><br class=""></div><div style="color: rgb(0, 0, 0); font-family: Arial; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px;" class="">Rick Aurbach</div><div style="color: rgb(0, 0, 0); font-family: Arial; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px;" class=""><br class=""></div></div></div></span></div></span></div></span></div></span></div></span></div></span></div></span></div></span></div></span></div></span></div></span></div></div></div></div></div></div></div></div></div></body></html>