Refactor Your IOS Code Fast With These Xcode Tips
Want to learn ways to speed up your iOS app development time? Need to edit and improve code quickly for a deadline? Then you’ve opened the right article.
Here are 4. Xcode refactoring tools to know to help you do just that.
1. Extract to Function
Have you ever started writing one function but then realized it grew to a thousand lines long? Then you need likely need to extract much of that code to its function. The extract-to-function feature makes it so easy to do this.
Use this feature by first highlighting the block of code you want to extract. Then right click and choose “Refactor” > “Extract to Function”
As the following gif demonstrates
2. Extract to Variable
An important principle many of us were likely taught to follow is that of using variables throughout the code instead of hardcoded values. And thats certianly right since it makes life much easier.
But what should you do if you realize half of your code length is because of. hardcoded names?
That's when extract to variable comes to the rescue! This refactoring feature extracts any portion of code to its own variable and replaces all instances of that code with the variable name you choose
Access the extract to variable feature by highlighting the block of code you want to extract. The right-click> “Refactor” > “Extract to Variable”. If you have mutliple occurences of this string, you can select “Extract all Occurrences”.
An example of this is shown below
3. Multi-line editing
Your in your zone happily coding and then a horrifying thought hits you. You forgot to rename 10 variables. You shudder at the thought of having to do all that work.
That.. doesn't have to be you.
With multi-line editing in Xcode, you can edit multiple lines at once. This allows you to edit variable names at once. However, they have to be on consecutive lines.
Start the multi line editing feature by holding the option key and then dragging the lines you want to edit. Youll see a blinking cursor and then you can start editing.
If what I just wrote looks like hieroglyphics. then look at the following gif.
4. Expand Switch Cases
Have you ever started making a switch statement but realized that you'll have to make more cases than Sherlock Holmes? Thankfully, refactoring tools come to the rescue again.
“Expand Switch Cases” is a refactoring feature that allows you to quickly expand all cases in a switch statement without all the typing.
To use this feature, simply place your cursor within the switch statement, right-click, and choose “Refactor” > “Expand Switch Cases” from the context menu. Xcode will then expand all cases within the switch statement, showing the associated code blocks.
The following GIF demonstrates this.
Xcode refactoring tools are truly invalauble when programming. They help us remember that computers sometimes, just sometimes, understand we are humans too.