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’.