michaeltyson / xcode-tips.github.io

Community-run website for documenting Xcode Tips

Home Page:https://xcode-tips.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Have a cool tip to share? Send a pull request or open an issue!

About

Folks in the Apple developer community are always sharing great Xcode tips — usually via Twitter or blog posts. Wouldn't it be nice if we collected them all in a single place to share? The goal of this project is to host all of these Xcode tips in a single place, and make it easy for anyone to contribute.

Contents

💡 Tip: we recommend following the "source" links for each tip to learn even more.

Use breakpoints as "bookmarks"

Any time I am exploring or getting familiar with a new codebase in Xcode, especially very large projects, I use disabled breakpoints as “bookmarks” to keep track of where I am, where I have been, and things I want to remember or need to revisit. Sometimes I even do this when debugging issues in codebases that I know well.

Source: Jesse Squires

Quickly toggling breakpoints

Use Cmd+\ to toggle a breakpoint on the current line.

Source: Paul Hudson

Play sound files in breakpoints

Audible Xcode breakpoints allow a sound effect to be played while continuing the executable without interruptions. This can be a really useful debugging tool. Custom Xcode breakpoint sound files can be found on GitHub at Xcode Breakpoint Sounds.

Source: Matt Reagan

Fix slow codesigning

Poking around, I came across this thread. While I didn’t have the main problem described there of duplicate certificates, buried in that thread was the following advice: trim ~/Library/Preferences/com.apple.security.plist

Opening that file up revealed that I had several entries, all except one pointing to non-existent files with the one valid entry pointing to my login keychain. After removing the invalid entries, code signing only took up to 1 second, max. This shaved 40-60 seconds off of my full release builds and 10 seconds off of incremental ones. Huge savings.

Source: Paul Kim

Copying framework headers

Another thing I noticed in the cleanup was that some of my frameworks were being copied without their headers. [...] Apparently, there’s a hidden setting in your project.pbxproj file for copying frameworks where you can specify whether headers get copied over.

The only way to enable/disable this is to edit the project.pbxproj by hand. The flag in question is RemoveHeadersOnCopy.

Source: Paul Kim

Generating class initializers

Xcode can generate class initializers. Select your class name, then go to the Editor menu and choose Refactor > Generate Memberwise Initializer.

Source: Paul Hudson

Selecting blocks of code

You can double click a brace to select the entire block of code it contains.

Source: Paul Hudson

Apply all fix-its

Go to the Editor menu and choose Fix All Issues to apply fix-its all at once.

Source: Paul Hudson

NSDoubleLocalizedStrings and Friends

The NSDoubleLocalizedStrings user default is a reasonably well-known and officially documented localization debugging aide. It repeats the text of each localized string, making it double-length so that you can test whether your layout still works.

Another longstanding one is NSShowNonLocalizedStrings, which logs to Console when a string can’t be found.

Interface Builder also lets you preview views using an “Accented Pseudolanguage” and a “Bounded String Pseudolanguage.” These correspond to the NSAccentuateLocalizedStrings and NSSurroundLocalizedStrings user defaults.

Finally, there are NSForceRightToLeftLocalizedStrings and AppleTextDirection to enable the “Right to Left Pseudolanguage.” This lets you use test right-to-left layout (e.g. for Arabic) using strings from your development language.

Source: Michael Tsai

Viewing .crash files

In Xcode’s Organizer, in the Crashes section, you can right-click or ctrl-click on any row and choose Show in Finder. This will reveal a .crashpoint file — do a Show Package Contents and then dig in further. You will find .crash files with the full crash logs, which provide a lot more info than what you see in Organizer.

Source: Brent Simmons

Faster Xcode Rename Refactoring

If you use the rename refactoring in Xcode a lot, you can save some time by skipping the code folding animation:

defaults write com.apple.dt.Xcode CodeFoldingAnimationSpeed -int 0

Source: Daniel Martín (via Michael Tsai)

Deleting search results

When you search using Xcode’s find navigator, you can click on individual results and tap Backspace to remove them.

Source: Paul Hudson

Customizing the file header comment and other text macros

Xcode allows you to customize the file header and other so-called text macros using a plist file.

  1. Create a property list file named IDETemplateMacros.plist.
  2. For every text macro you want to customize, add a new key to the plist’s dictionary.
  3. Copy the file to one of the following locations.
    1. <Name>.xcodeproj/xcshareddata/
    2. <Name>.xcworkspace/xcshareddata/
    3. ...

Source: Ole Begemann

Improving the assistant editor

  1. Set “Uses Focused Editor” in the Navigation preferences
  2. cmd-J to switch between panes or open new ones
  3. cmd-shift-O to open files in the currently focused pane

Source: Jesse Squires

Navigation using the assistant editor

The assistant editor is very useful for navigating around while remaining at your original position:

  • Use cmd option , to open the current location in the neighbouring editor
  • Hold cmd ctrl option and click on any method to jump to that method in the neighbouring editor (cmd ctrl opens in the current editor)
  • cmd ctrl UpArrow to switch between associated files, and cmd ctrl option UpArrow to do so using the neighbouring editor
  • cmd ctrl LeftArrow and cmd ctrl RightArrow to move back and forward through navigation history, add option for their neighbouring editor counterparts

Using behaviors to improve debugging

In Xcode’s preferences, go to the Behaviors tab. Navigate to the ‘Running’ section and click ‘Pauses’. Here you can instruct Xcode to open a new tab by checking the box for ‘Show tab named’ and giving it a name. By default, showing the ‘Debug Navigator’ should be enabled. Next, I like to show the debugger with the ‘Variables & Console View’, as well as hide the Utilities sidebar on the right.

Source: Jesse Squires

Jump to a specific line

Open the file you want. Press Cmd+L, type a line number and Xcode will jump directly to that line.

Reindenting/Formatting code

Press Ctrl+I to apply Xcode's indentation and formatting.

Adding comments quickly

Use Cmd+/ to toggle comments for the current line or selection. Use Option+Cmd+/, pressed directly before a method to have Xcode generate a documentation comment.

Source: Paul Hudson

Jump to file in source navigator

Press Cmd+Shift+J to quickly jump to the current open file in the navigator to easily see and select related files.

Source: Jeroen Leenarts

Open the jump bar

Press Ctrl+6 to open the symbol jump bar in Xcode. Now start typing. Try it, jumping to a function in the current file, never has been so easy.

Source: Jeroen Leenarts

Remapping unhelpful keys

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.

Source: Paul Hudson

Tiling the simulator

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.

Source: Paul Hudson

Status bars

Clean up and configure Simulator status bars using simctl status_bar, automate using Nine41.

Source: Jesse Squires

Re-run your last test

Use Ctrl+Opt+Cmd+G to re-run your last test. Jon Reid has a name for this making it easier to remember: “smash go!”

Source: Paul Hudson

Randomizing test order

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.

Source: Paul Hudson

Improving UI test reliability

You can disable or speed-up animations, and increase timeouts for waitForExistence().

Source: Jesse Squires

Testing in-app purchases

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.

Source: Paul Hudson

Expanding autocomplete

You can grab the edge of the autocomplete popup and drag it as wide as you want!

Source: Paul Hudson

Generating an interface file

Press Ctrl+Cmd+Up to display a generated interface, showing properties, function signatures, and comments for a type. Press it again, to jump to tests for that file if they exist.

Source: Paul Hudson

Fix Freezing

If your Xcode freezes a lot, unpair all devices (Window/Devices and Simulators).

Source: Michał Januszewski

Make Xcode's Assistant aware of your ViewModels, Views, etc

defaults write com.apple.dt.Xcode IDEAdditionalCounterpartSuffixes -array-add "ViewModel" "View" "Screen"

You can check the current value of this default using defaults read com.apple.dt.Xcode IDEAdditionalCounterpartSuffixes.

Source: Peter Friese

Prevent restoring the last open project

This is useful if you have a project that crashes Xcode on launch, if you want to run multiple Xcode versions for different projects, or if you always want to choose the project to open.

defaults write com.apple.dt.Xcode ApplePersistenceIgnoreState -bool YES

Source: Txai Wieser

Show project build times in the activity viewer

This shows the build time duration directly in the activity viewer every time you build.

defaults write com.apple.dt.Xcode ShowBuildOperationDuration -bool YES

Source: Txai Wieser

Quickly switching between Xcodes

Using plain xcode-select is slow because you have to provide the path to the Xcode you want to select each time. I wrote a custom shell command to switch between Xcodes more quickly.

Source: Jesse Squires

Install, manage and switch between different Xcode versions

An easy-to-use command line tool to install and uninstall different Xcode versions on your machine. Xcode versions are installed side-by-side with the version in their name and makes downloading/installing them incredibly easy.

Source: xcinfo

About

Community-run website for documenting Xcode Tips

https://xcode-tips.github.io

License:MIT License