Introduction to Xcode

Apr 24 2024 · Swift 5.10, iOS 17, Xcode 15

Lesson 02: Debugging & Troubleshooting in Xcode

Warnings Demo

Episode complete

Play next episode

Next
Transcript

If you want to follow along, open the demo app from the Final folder in the materials for this lesson.

Start by adding Quick Help documentation to the dogImages variable in the demo app. Right-click or control-click the symbol and select Add Documentation from the context menu. Replace the Description placeholder with some useful comment.

Now scroll down to where the dogImages variable is used in the code. Click the variable and then look at the Quick Help inspector for your comment.

Next, add the button increment code to a snippet so you can reuse it elsewhere. First, highlight the code you want in the snippet. Now, right-click to show the context menu and select Create Code Snippet.

You can give the snippet a title and a short description. In the Completion field, you can make the keyboard shortcut for the snippet. To keep your snippet shortcuts from colliding with the system shortcuts, add a character or two at the beginning to help organize them. After making the shortcut, you can replace the values specific to this instance of the snippet with more general placeholders.

Turn maxIndex into a placeholder by surrounding it with <# and #>. Now, save your snippet and try it out. Erase the button action code and type your snippet shortcut. Autocomplete shows your snippet. You can select it and update the placeholder.

To show a Fix-It, add a question mark ? at the end of self on any lines that read self.currentIndex.

Xcode shows you an error about optional chaining. Click the error icon to see the Fix-It options. Now click the Fix button so that Xcode removes the question mark and the error.

To make your own warning appear, type #warning("fix this later"). The warning appears. If you open the Issue navigator, you’ll also see the warning there. Sometimes, you might want to add a TODO or FIXME comment without making it a warning. You can put a few special keywords at the beginning of your comments to make them appear in the navigation menu.

On a blank line, type a comment like "//TODO: Fix this later". Notice how Xcode recognizes TODO as a special word and puts the comment in bold.

The comment also appears in the navigation. Type Control-6 to show the navigator and see a special icon in the list.

Other comments that put special icons in the list are // FIXME:, // !!!: and // ???:. You can also use // MARK: - to add a divider to the list and // MARK: - Title to add a divider with a flag. Remember from the earlier lessons you can use the Find navigator with the Bookmark navigator to gather all of these comments into one place.

Now, create some false positive errors. Remove one of the closed curly braces early in the file. Then, move your cursor to a different line. The Xcode analyzer pauses when you’re on a line so that it doesn’t annoy you by showing errors as you type.

Sometimes the analyzer forgets to restart. If you ever find yourself waiting for more than a few seconds, moving to the next line usually makes it start up again. See how there are many errors in the file, but the cause of the errors isn’t highlighted at all.

Some errors, like "Consecutive statements...", have Fix-Its in them. But applying the fixes will make things worse. So, get in the habit of checking the ends of the file first and then work from top to bottom to fix errors.

Finally, you need to know where to change the build setting to enable hard mode and set all warnings as errors. In the Project navigator, select the project icon and then select the target.

Now click Build Settings. There are many of them, so it’s best to use the text filter to search for the ones about warnings. Now scroll down to the Warning Policies.

There are two compilers in the Xcode project. Find the setting that goes with the Swift compiler, not the Apple Clang compiler. Notice that you can’t change the “Resolved” entry.

Build settings override as you move from Apple default to Project to Target. “Resolved” shows what value the build system is actually going to use.

You must select the Target or Project entry and change it to Yes.

Now, notice that the #warning you added earlier is an error.

See forum comments
Cinema mode Download course materials from Github
Previous: Preventative Measures Next: Breakpoints