Hallo!
Ich arbeite gerade das Buch "Swift Development with Cocoa" durch und es gibt da eine Stelle, aus der ich nicht schlau werde.
Es geht um die init()- und deinit-Funktion in Klassen. Ich habe meiner Meinung nach alles richtig geschrieben, aber der Playground will mir den Inhalt von deinit nicht ausgeben (println("Bye!")), nachdem ich die Variable, in der das Objekt gespeichert war, auf nil gesetzt habe.
Ich arbeite gerade das Buch "Swift Development with Cocoa" durch und es gibt da eine Stelle, aus der ich nicht schlau werde.
Es geht um die init()- und deinit-Funktion in Klassen. Ich habe meiner Meinung nach alles richtig geschrieben, aber der Playground will mir den Inhalt von deinit nicht ausgeben (println("Bye!")), nachdem ich die Variable, in der das Objekt gespeichert war, auf nil gesetzt habe.
Code:
[COLOR=#BB2CA2][FONT=Menlo]class[/FONT][/COLOR][FONT=Menlo] InitAndDeinitExample {
[/FONT][FONT=Menlo] [COLOR=#bb2ca2]init[/COLOR]() {[/FONT]
[COLOR=#D12F1B][FONT=Menlo][COLOR=#3d1d81] println[/COLOR][COLOR=#000000]([/COLOR]"I've been created"[COLOR=#000000])[/COLOR][/FONT][/COLOR]
[FONT=Menlo] }[/FONT]
[COLOR=#BB2CA2][FONT=Menlo]convenienceinit[COLOR=#000000](text:[/COLOR][COLOR=#703daa]String[/COLOR][COLOR=#000000]) {[/COLOR][/FONT][/COLOR]
[FONT=Menlo] [COLOR=#bb2ca2]self[/COLOR].[COLOR=#bb2ca2]init[/COLOR]()[/FONT]
[COLOR=#D12F1B][FONT=Menlo][COLOR=#3d1d81] println[/COLOR][COLOR=#000000]([/COLOR]"Now with the convenience initializer"[COLOR=#000000])[/COLOR][/FONT][/COLOR]
[FONT=Menlo] }[/FONT]
[FONT=Menlo] [COLOR=#bb2ca2]deinit[/COLOR] {[/FONT]
[FONT=Menlo] [COLOR=#3d1d81]println[/COLOR]([COLOR=#d12f1b]"Bye!"[/COLOR])[/FONT]
[FONT=Menlo] }[/FONT]
[FONT=Menlo]}[/FONT]
[FONT=Menlo]
[/FONT]
[COLOR=#4F8187][FONT=Menlo][COLOR=#bb2ca2]var[/COLOR][COLOR=#000000] example : [/COLOR]InitAndDeinitExample[COLOR=#000000]?[/COLOR][/FONT][/COLOR]
[COLOR=#4F8187][FONT=Menlo]example[/FONT][/COLOR][COLOR=#000000][FONT=Menlo] = [/FONT][/COLOR][COLOR=#4F8187][FONT=Menlo]InitAndDeinitExample[/FONT][/COLOR][COLOR=#000000][FONT=Menlo]([/FONT][/COLOR][COLOR=#000000][FONT=Menlo])
[/FONT][/COLOR][COLOR=#4F8187][FONT=Menlo]example[/FONT][/COLOR][COLOR=#000000][FONT=Menlo] = [/FONT][/COLOR][COLOR=#bb2ca2][FONT=Menlo]nil[/FONT][/COLOR]
[COLOR=#4F8187][FONT=Menlo]example[COLOR=#000000] = [/COLOR]InitAndDeinitExample[COLOR=#000000](text:[/COLOR][COLOR=#d12f1b]"Hello"[/COLOR][COLOR=#000000])[/COLOR][/FONT][/COLOR]
[COLOR=#4F8187][FONT=Menlo]example[COLOR=#000000] = [/COLOR][COLOR=#bb2ca2]nil[/COLOR][/FONT][/COLOR]