Spell checking in Xcode

⋅ 5 min read ⋅ Xcode Development

Table of Contents

One hidden feature in Xcode is its support for spell checking. Today, I will show you how to enable it and explore some capabilities.

How to enable spell-checking in Xcode

You can enable this feature by going to Edit menu > Format > Spelling and Grammar > Check Spelling While Typing.

After you enable spell checking, Xcode will underline any typo with a red underline.

Xcode will underline a misspelled word in red.
Xcode will underline a misspelled word in red.

How to detect the typo

As mentioned earlier, the misspelled word will be highlighted with a red underline. But it is hard to notice all the words if you have a large file. An easy way to find all misspelled words is by using the following command:

⌘ - command + ; or
Edit menu > Format > Spelling and Grammar > Check Document Now.

Check Document Now command will find and select a misspelled word. Repeat this command will jump to the next one. So you can see every typo in the current file with this method.

command + ; to find next misspelled word.
command + ; to find next misspelled word.

How to fix the typo

There are two ways to correct misspelled words.

Context menu

Right-click or Control-click on the misspelled word will open a menu with suggested corrections. Click any of them to choose.

Right-click and choose correction from the suggested list.
Right-click and choose correction from the suggested list.

Fixing typo this way would be very slow if you have a large number of misspelled words. The faster way would be using Spelling and Grammar window.

Spelling and Grammar window

To fix typo with this method, first, you need to open the Spelling and Grammar window.

Open Spelling and Grammar window with the following shortcut:

⌘ - command + : or
Edit menu > Format > Spelling and Grammar > Show Spelling and Grammar.

This window will show up over your Xcode.

Spelling and Grammar window
Spelling and Grammar window

We have many options here.

  1. Change. Select the suggested word from the bottom and click Change to fix the text. This is equivalent to right-click and select.
  2. Find Next. This will select the next mispelled word. This is equivalent to ⌘ - command + ; shortcut.
  3. Ignore. This option will ignore the selected item, but it doesn't work with Xcode. Once you ignore, the red underline will be gone, and that word will be treated as correct, but if you leave that file and come back, the ignore-state isn't saved, and the word will be treated as misspelled again.
  4. Learn. This option will add the selected item to the dictionary, and the word won't treat as misspelled again.
  5. Define. This will look up the selected item in the dictionary.
  6. Guess. This will suggest possible corrections for this item.

But we won't use any of that options when doing the correction.

To quickly fix every mistake in the file, we do the following:

  1. Open up the Spelling and Grammar window.
  2. Then, we use Check Document Now (⌘ - command + ;) to jump to each word.
  3. Double click the correction that you want from the list.
  4. Spelling and Grammar will automatically jump to the next word. Then you repeat step 3.
Double click on the correction that you want to apply the fix.
Double click on the correction that you want to apply the fix.

Capabilities

Spelling in coding is not straightforward as regular writing. We have a unique way to style our variables, methods, and classes. We will learn in this section whether the spell checking can support camelCase, snake_case, PascalCase, and kebab-case.

I test by declaring variable names with all four cases.

var camellCase: String = "Camell Case"
var PascarCase: String = "Pascal Case"
var snakee_case: String = "Snake Case"
var kebabCase: String = "keba-case"

enum Foo: String {
case isVisible = "is_vissible"
}

Here is the result. Xcode can detect variables or strings regardless of cases.

Spell checking in Xcode.
Spell checking in Xcode.

Be able to detect typos from different cases means it is case sensitive. This makes it can catch wrong capital cases like "VIew".

Spell checking in Xcode is case sensitive.
Spell checking in Xcode is case sensitive.

How to add a new word

You might have a unique word that not define in the system dictionary, such as your app name or prefix. In this case, you can add a new word to the dictionary.

You can make your dictionary learn a new word by right-clicking and select Learn Spelling.

Select Learn Spelling to add a new word to the dictionary.
Select Learn Spelling to add a new word to the dictionary.

Another way to do it is via the Spelling and Grammar window by clicking the Learn button.

Click learn button to add a new word to the dictionary.
Click learn button to add a new word to the dictionary.

How to unlearn a word

If you accidentally learn a wrong word or want to revert your decision, you can unlearn the word, but it is not a straightforward operation for Xcode.

Apps that fully support spell checkings, such as the Pages and Notes app, can unlearn the word, but Xcode isn't one of this kind.

Right-click on the learned word on the Pages app, and you will see an Unlearn Spelling option.

Unlearn option will show up in the popup-menu for apps that fully support spell checking.
Unlearn option will show up in the popup-menu for apps that fully support spell checking.

If you right-click on the learned word on Xcode, the Unlearn Spelling option won't show up. In my case, I learn the word "Sarunw", but when I right-click to unlearn, the option isn't there.

Xcode doesn't fully support every feature of spell checking.
Xcode doesn't fully support every feature of spell checking.

How would you unlearn the word then? We have two hacky ways to do this.

Quick and dirty

The quick and dirty solution is to copy the word, paste it into the Pages or Notes app, right-click and choose Unlearn Spelling from the pop-up menu.

Bulk Edit

If you have a big chunk of falsely learn words, manually copy and unlearn it one-by-one might not be possible. You can edit the list of learned words directly by following these steps:

  1. Open LocalDictionary located at ~/Library/Spelling.
  2. You will see a list of learned words. Remove the one you want to unlearn and save the file.
  3. To make the change reflect in Xcode, we need to restart the AppleSpell process. You can do that by launch Activity Monitor from /Applications/Utilities. Then search for AppleSpell. Select it and click the X button on the toolbar.
  4. Select Force Quit.

After you quit AppleSpell, it restarts automatically, and the changes made will reflect in Xcode.

You can easily support sarunw.com by checking out this sponsor.

Conclusion

In this article, we learn how to enable spell-checking in Xcode, quickly find and fix our typos, and learn and unlearn technical terms in our app.

The best part about this feature is it can detect typos on class names, variables, not just a string. It also catches misspelled word writing in all cases that we normally use in codings, such as camelCase, snake_case, PascalCase, and kebab-case.


Read more article about Xcode, Development, or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
How to explicitly specialize a generic function in Swift

Learn a workaround to specify a type for your generic functions.

Next
How to ignore safe area insets in UIKit

Learn how to make UIScrollView, UITableView, and UICollectionView ignore safe area insets and put their content behind that navigation bar.

← Home