Absolutely Crashing: The Land of OO -Â âOrphaned Outletsâ
Swift 2 (Swift 2.2), Xcode7
When youâre using Xcode to make your app, youâll see errors and warnings pop up in the gutter of your code, letting you know where problems are, or could be in the future. But sometimes we run the app in Simulator and it crashes, even if the code looks fine. Broadly speaking, this is the difference between âCompiler Errorsâ (errors that need to be fixed before the code can be run) and âRuntime Errorsâ (errors that occur when we are running the app).
When this happens we open our debugger. At the top right of Xcode are the buttons shaped like rectangles with lines in them, which we use to show or hide the Navigator (left) and Utilities (right). The button with the line at the bottom shows the Debug area.
When our app crashes, it should come up with an error message in the right of the debug menu. These error messages, as youâve probably got used to with Xcode, come in a wide range of usefulness, from telling you what the problem is, to spitting out gibberish and leaving you on your own.
Weâre going to cover one of those confusing errors today. Specifically a thing called an âOrphan Outletâ. These are reasonably common, and cause your pretty top-tier crashes, before it even seems to be touching the code that youâve written.
It might look something like: Terminating app due to uncaught exception âNSUnknownKeyExceptionâ, reason: [insert specific for you information here]: this class is not key value coding-compliant for the key [name of Orphaned Outlet].
So, what is an âOrphaned Outletâ, and why do they happen so frequently?
Well when we make things in Main.storyboard (thatâs our visual app interface), we get used to making Labels and Buttons and those sorts of things, pulling up the Assistant Editor (the bit that pulls your code up alongside) and ctrl-dragging one of these elements into our code, connecting them. All well and good.
However sometimes we think, âActually, I donât need that Label connected anymoreâ or âI donât really need that codeâ and delete the lines of code that that process added.
This creates the âOrphaned Outletâ, because behind the scenes itâs still all connected, and the app gets cranky when trying to put things together that arenât in the code anymore.
How do we check for them and get rid of them then?
We can use the âConnection Inspectorâ (6th tab in your right-hand Utilities, or alt-cmd-6) to show all of our connections when we click on each element. It will be pretty obvious if the element has a connection - it will be ringed in darker grey. These tell you what it is known as in the code e.g. myLabel, and the section it relates to, e.g. View Controller. This means you should be able to fairly easily search through your code and see if itâs used if youâre not sure.
If you find an OO, then you can delete it. This means going to that grey ring and clicking the little grey X next to the section label.
Or you can right-click (/two-finger click) on the element in your app display (e.g. main.storyboard) and do all of that from there, without the Connection Inspector. Eitherâs good.
If you know youâve been doing a lot of connecting, deleting and messing around with elements, and youâre getting a confusing sounding crash, then it might be worth having a check for some âOrphaned Outletsâ.












