24 XCode tips by @TwoStraws

From Paul Hudson ( @twostraws - Hacking with Swift ) on twitter 2020 Dec. one a day… they include videos.


Article: Part One - https://www.hackingwithswift.com/articles/166/xcode-tips-and-tricks-part-one



Xcode tip #1: Swift can synthesize memberwise initializers for structs, but not for classes. Fortunately, Xcode can generate the code for us: select your class name, then go to the Editor menu and choose Refactor > Generate Memberwise Initializer. Done!

https://twitter.com/twostraws/status/1333828457579483136?s=20



Xcode tip #2: When building software, it’s important to make sure your app works for *everyone*. One great way to do that is with Xcode’s environment overrides panel, which lets you adjust Dynamic Type size, accessibility options, and appearance all in one place.

https://twitter.com/twostraws/status/1334190845398974464?s=20



Xcode tip #3: You probably already know that you can click after any open { or close } brace to have Xcode highlight its matching brace, but another useful tip is that you can double click a brace to select the entire block of code it contains.

https://twitter.com/twostraws/status/1334553233239535616?s=20



Xcode tip #4: Xcode can check if your spelling is correct, and it even understands that camelCaseNames should be treated as separate words. Go to the Edit menu and choose Format > Spelling and Grammar > Check Spelling While Typing, and Xcode will check your code for typos.

https://twitter.com/twostraws/status/1334915621038026755?s=20



Xcode tip #5: If you have a Swift file with many errors, you can use Xcode’s fix-its to try to correct them one by one. But a faster way is to go to the Editor menu and choose Fix All Issues to apply fix-its all at once. (Make sure and double-check which changes were made!)

https://twitter.com/twostraws/status/1335278008865787907?s=20



Xcode tip #6: When you search using Xcode’s find navigator, you can click to view individual results and make any changes you want. But when you’re done, I recommend you hit Backspace to remove the search item from the results, so you can see which results you have left to check.

https://twitter.com/twostraws/status/1335640396605452289?s=20



Xcode tip #7: This is the simplest tip I know, but it will save you time if you don’t already know it. Whenever you see Automatic Preview Updating Paused in your SwiftUI canvas, press Opt+Cmd+P to resume previewing. Alternatively, use Opt+Cmd+Return to hide the canvas completely.

https://twitter.com/twostraws/status/1336002784743702529?s=20



Xcode tip #8: If you frequently move from Xcode to the simulator, tile them side by side. With the simulator active, go to the Window menu and choose Tile Window To Right Of Screen, then select Xcode on the left. You can adjust the split so the simulator sits snugly on the right.

https://twitter.com/twostraws/status/1336365172647211009?s=20


Xcode tip #9: Xcode has great code completion built right in, but sometimes as you scroll through the options you might find the names are too long to fit. Fortunately, you can just grab the edge of the autocomplete popup and drag it as wide as you want!

https://twitter.com/twostraws/status/1336727560424542209?s=20



I've mentioned this before, but it bears repeating: you can put images into SwiftUI text views, and concatenate them into the rest of your text


Xcode tip #10: Breakpoints are great for debugging – place them by selecting a line number, or remove them by right-clicking and choosing Delete Breakpoint. You can also remove one by dragging it away, but a fast method is to use Cmd+\ to toggle a breakpoint on the current line.

https://twitter.com/twostraws/status/1337089948378271744?s=20


Tiny Xcode 10 tip that keeps being useful: click on the object library to bring up the popover, but Alt+click to make it a window you can drag around freely and reuse.

https://twitter.com/twostraws/status/1052286968925736960?s=20


Xcode tip #11: Sometimes the output from one test affects the input for another. Xcode has a solution: go to the Product menu, hold down Option, then click Test. Inside the Info tab, click Options then check Randomize Execution Order to run tests in a different order every time.

https://twitter.com/twostraws/status/1337452336168493060?s=20


Xcode tip #12: Xcode’s jump bar (Ctrl-6) is the best way to jump around long files quickly, letting you browse all properties and methods in your file. For longer files, start typing with the jump bar open and Xcode will filter it – a trick that also works for device selection.

https://twitter.com/twostraws/status/1337814723987828738?s=20


Xcode tip #13: It’s common to want to get an overview of how some code works. If you press Ctrl+Cmd+Up Xcode produces a generated interface, showing properties, function signatures, and comments. If you press it again, Xcode takes you to the tests for that file if they exist.

https://twitter.com/twostraws/status/1338177111987691520?s=20


Xcode tip #14: One of my most commonly used Xcode shortcuts is Cmd+/ to toggle comments for the current line or selection, but another useful one is Option+Cmd+/ – press that directly before a method to have Xcode generate a documentation comment, including its parameters.

https://twitter.com/twostraws/status/1338539499664662530?s=20


Xcode tip #15: As your projects grow, it gets harder to use the project navigator to find something. There are two solutions: one is to right-click on any file group and choose Sort By Name to arrange them A-Z, and the second is to type into the filter box at the bottom.

https://twitter.com/twostraws/status/1338901887672872966?s=20


Xcode tip #16: Xcode can handle source changes right in its editor. To try it, enable Show Source Control Changes in Xcode’s preferences, then make a change. On the left you’ll see a blue bar, but if you click it and select Show Change Xcode shows you the old and new code inline.

https://twitter.com/twostraws/status/1339264275471187969?s=20


Xcode tip #17: Xcode’s minimap helps us browse long files – enable it from Editor > Minimap. As you hover your mouse over it, the minimap tells you the name of the method you’re over. You can also hold Cmd to reveal the names of everything, then click on a name to jump to it.

https://twitter.com/twostraws/status/1339626663466987521?s=20


Xcode tip #18: It’s common to write failing tests, particularly when using TDD. Fortunately, Xcode has a keyboard shortcut to run only the last test, which is faster than running everything: Ctrl+Opt+Cmd+G. @qcoding has a name for this making it easier to remember: “smash go!”

https://twitter.com/twostraws/status/1339989051152224259?s=20


Xcode tip #19: Some great shortcuts (e.g. Shift+Cmd+O for Open Quickly) are next to useless shortcuts (Shift+Cmd+P, for the never times you want to print code.) It takes only seconds to remove unhelpful keys, and you can even remap things like Cmd+P to resuming SwiftUI's preview.

https://twitter.com/twostraws/status/1340351439063797763?s=20


Xcode tip #20: All the titles in Xcode's find navigator can be changed: Find can Replace, Text can be references or regular expression, and Containing can be matching, starting, and more. You can also click the magnifying glass to see recent searches – selecting one repeats it.

https://twitter.com/twostraws/status/1340713826749042688?s=20


Xcode tip #21: If you’ve copied code from elsewhere, such as Hacking with Swift or Stack Overflow, there’s a good chance you’ll paste in something with incorrect indentation. Xcode can fix this with one shortcut: select the code you want to fix, then press Ctrl+I to reindent it.

https://twitter.com/twostraws/status/1341076214774194176?s=20


Xcode tip #22: You can test in-app purchases without App Store Connect. Make a new StoreKit Config File, and add your IAP. Now go to the Product menu, hold down Option, and click Run. From the Options tab of the window, change StoreKit Config, and now you'll use the test IAP.

https://twitter.com/twostraws/status/1341438602614624259?s=20


Xcode tip #23: There are many settings to control how Xcode builds your code, but it’s hard to remember what they all do. Fortunately, you can select one and use the Quick Help inspector to see documentation for most settings, or hold Option and double-click to get inline help.

https://twitter.com/twostraws/status/1341800990492782593?s=20


Xcode tip #24: The canvas is a great way to preview layouts as you work, but it’s harder to use when one view is split across files. To make things easier, use the pin at the bottom to keep the current preview active, allowing you to change one file while previewing another.

https://twitter.com/twostraws/status/1342163378350010371?s=20