-
Notifications
You must be signed in to change notification settings - Fork 472
Common Issues
While learning to develop in iOS, you will undoubtedly encounter many issues. Fortunately, many of these issues are caused by a handful of very common issues. Awareness of these common pitfalls along with knowledge of how to investigate clues given to us by Xcode, will help to get us out of trouble when we have errors or crashes.
There are two main categories of errors, Compiler Errors and Run-Time Errors.
- Symptoms: You have little red stop signs in your file margins. As a result, Your app fails to build.
- Diagnose: Click on the red stop signs to get hints on what and where the issue is.
- Symptoms: Your app builds fine, but at a certain point it "crashes".
-
Diagnose: Go to the Debug area pane at the bottom of Xcode.
- 1: The Console, (bottom right window), will likely have a long list of confusing "computer talk", fortunately, the useful information is found in the first paragraph all the way at the top, so get scrolling!
- 2: The second place to look is in the Variables View to the left of the console.
When your app crashes, it's often due to an initial error. However, the initial error throws everything else after it off creating other errors. The result can look like a real mess and even point to "red herrings" that aren't directly involved, like the App delegate. To get to the root of our problem, we need to find the first error that caused our app crash.
The console will be our first source for error printouts. Scroll all the way to the top to see the initial error.
- Translation: This basically means that sometime while your app was running, it was looking for something and instead got nothing, nil.
- Common Cause An outlet was created but got disconnected.
-
Diagnose:
- Step 1: We find our first clue is in the console. It tells us that something has no (nil) value. s
-
Step 2: We check the Variables Window next. When we expand the self heading, we see that, pigImageView = (UIImageView!) nil, "nil" being the keyword. This means
pigImageView
has no value, and Xcode is telling us that it should. - Step 3: We go to the swift file where that variable is declared. We notice that the little circle to the left where we make connections is empty. AH HA! Our code was trying to do something with the pigImageView, but it was not connected to the actual imageView in the Storyboard.
- Solution: Reconnect by dragging from the empty circle to our pig imageView in Storyboard.
- Translation: Basically, there is a connection somewhere behind the scene that is looking for a specific outlet but is not finding it.
- Common Cause: The name of an outlet was changed or deleted but the connection is still there.
-
Diagnose:
-
Step 1: Scroll all the way to the top of the Console to find the useful information.
- The ViewController where the error is occurring.
- The Key Value Coding-compliant tells you there is a connections issue.
- the name after for the key is what you will be looking for when you investigate connections.
- Step 2: Select the offending ViewController by clicking on the yellow dot. Open the Utilities pane and select the Connections Inspector.
-
Step 1: Scroll all the way to the top of the Console to find the useful information.
- Solution: Delete the bad connection by clicking the little "x" between the connections. The bad connection should have an "!" on the right side instead of a little circle.
- Translation A custom ViewController Swift file you made is not configured right.
- Common Cause You assigned your ViewController to a Custom Class (In Utilities -> Identity Inspector) before actually creating the Custom Swift ViewController File.
-
Diagnose:
- Step 1: Check the console to see which ViewController file is having the issue.
- Solution: Click on the offending ViewController's yellow dot in Storyboard. Navigate to the Identity Inspector in the Utilities Pane. In the "Custom Class" section, click in the Module field. Press Enter, the Module field should change to "Current (Your App Name)" in a light grey text. Run your app.
- Solution: Click on View Controller that contains the Scroll View, navigate to the Attributes Inspector (DJ Slider) inside the Utilities Pane. Uncheck, "Adjust Scroll View Insets". This is a feature for iOS 6.0 compatibility and who cares about them!
- Solution: Click on the Image View where the Gesture Recognizer has been adde and navigate to the Attributes Inspector (DJ Slider) inside the Utilities Pane. Check, "User Interaction Enabled".
Breakpoints are an essential debugging tool, however it's easy to accidentally set a breakpoint and cause what appears to be a crash at run-time.
-Solution: Navigate to the "gutter" of the ViewController file that has the accidental break point (Far left side of the file). Right Click or ctrl + click on the blue breakpoint tag and select, "Delete Breakpoint" from the dropdown menu.
- Translation: There is a word that Xcode does not recognize entered as a variable or constant.
-
Common Causes:
-
- You forgot to define the variable first using var or as a constant using let.
-
- You did not define the variable or constant up top as a "global" variable and are trying to reference the variable in different places throughout your Swift file.
-
- You misspelled the variable or constant.
-
-
Solution: Define your variable or constant using
var
orlet
respectively. If you define your variable up top above theviewDidLoad
method, you can access that variable anywhere in your Swift file. If you define your variable within a method, you will only be able to access it within that method.
-Solution Change from Manual to Automatic in the Assistant Editor.