Understanding The "Use of Unresolved Identifier" Error In Xcode

Written by: Reinder de Vries, June 30 2017, in App Development

Use of unresolved identifier

It’s one of the most common errors in Xcode: Use of unresolved identifier. This error simply means that Xcode doesn’t mean what you’re talking about. But… how do you solve it?

This article lists 3 ways you can solve the Use of unresolved identifier error. With these 3 methods you can fix 90% of the causes of the error.

Let’s get started.

  1. What “Unresolved Identifier” Means…
  2. Solution 1: Check Your Code For Typos
  3. Solution 2: Google The Exact Error
  4. Solution 3: Use Autocomplete Or Quick Help
  5. Conclusion

What “Unresolved Identifier” Means…

Before we begin – what does the Use of unresolved identifier error exactly mean? You already know that debugging Xcode’s error messages can be pretty cryptic and unsurprisingly, this error message isn’t different.

In the error message “identifier” refers to a framework, a class, a function, a property or a local variable.

You can use these identifiers throughout your code, for instance to call a function on an instance of a class:

let dog = Dog(name: "Bob")
dog.sit()

When the class Dog (with a capital D) does not have a function called sit(), Xcode will show you the Use of unresolved identifier error.

Use of unresolved identifier

“Unresolved” means that Xcode wasn’t able to resolve the identifier – it couldn’t find it. You can’t code a variable or function if Xcode doesn’t know what you mean!

Xcode uses the Swift compiler to statically analyze your Swift code. With static analysis the compiler can spot bugs and errors by looking at your code, before you run your app. This helps you to code more productively, because you can spot errors early.

Use of unresolved identifier is such a compile-time error, because the error arises when compiling your app. Conversely, a run-time error is an error that occurs while you run your app.

So… Use of unresolved identifier really means:

“You’re using a framework, class, function, property, or variable Xcode doesn’t know!”

Get 5 of my best practices

Get 5 of my best practices

Learn how to build better iOS apps

I’ll show you exactly how I built a dozen professional iOS apps,
write extensible Swift code, and turn coffee into code.
Wait, what? Yup – into Swift code.

Solution 1: Check Your Code For Typos

As you know, programming is a pretty exact activity. When you make a typing mistake, you’ll get an error. Swift code is case-sensitive too, so myUserNameLabel isn’t the same as myusernamelabel.

It’s easy to make a mistake typing some code with your Mac’s keyboard, so your first strategy to solve the Use of unresolved identifier should be to check your code for typos.

Xcode often tells you on which line an error occured. With the red bar it’ll highlight a line of code in one of your Swift files. Unfortunately, this isn’t always the line of code you need to look at!

Consider the following scenario:

  • You’ve created a property called textlabel at the top of your view controller class, so you type:
    var textlabel:UILabel?
    
  • In the method viewDidLoad() you want to access the property, to set it’s text, and you type:
    textLabel?.text = "Hammock chicharrones ugh kovfefe"
    

See what’s going on here? Even though the second line of code is essentially correct, it’ll trigger an error because the textLabel property doesn’t exist – you mistyped it on the first line as textlabel without the capitalized L

Use of unresolved identifier

Even though Xcode tries to be helpful by indicating the offending line of code, it can’t know for sure what you meant in the first place: textLabel with a capital L!

Want to know the best defense against typos? Good code formatting and following conventions. If you always do something a particular way, it’s harder to make mistakes. Here’s some ideas:

  • Always use “camelCase”, so capitalize your variables, instances and functions per word: textLabel, locationManager, imageData. You always start with a lowercase character.
  • Always capitalize the first character of a class or struct: Doge, DetailViewController, String.
  • Avoid using underscores, it’s unconventional for Swift and Cocoa Touch.
  • You must always “balance” brackets { } ( ) < > [ ], so when you open a bracket, you also have to close it. Mistakes in brackets aren’t always clear.
  • Properly indent your lines of code. It’s easiest to indent with 4 spaces or 1 tab after every opening squiggly bracket {, and to “go back” one indent after closing with }. There’s more to it though…

Build better iOS apps by mastering best practices and app architecture » Find out how

Solution 2: Google The Exact Error

Typos aren’t the only cause of the Use of unresolved identifier error! Xcode indicates it can’t find the property or variable you’re using, but that doesn’t mean you made a typo.

One common cause is that you simply forgot to import the right classes or frameworks:

import Alamofire

If you’re using a function from the Alamofire framework, but you haven’t imported it at the top of your Swift file, you’ll definitely see the Use of unresolved identifier error.

A way to find out that you’ve in fact forgotten to import a framework is to simply Google for the exact error you’re seeing in Xcode:

Use of unresolved identifier 'Alamofire'

Use of unresolved identifier

When you do that a great number of results show up, and you’ll have to sift through them to see if a solution works for you. Don’t copy and paste code, though! Here’s some things that came up:

  • They’ve replaced something in the framework’s code, so code that worked before won’t work anymore if you upgraded your framework version
  • When using a “wizard” for setting up integration with Firebase, they’ve used a newer class called FirebaseApp when in fact your code still runs on the older version of the framework that uses FIRApp
  • You’re using an Objective-C library, but you made a typo in the Bridging Header, so now when you try to import the library, it won’t work!

When you get used to searching on the internet for causes of errors, you’ll quickly get better at sifting through StackOverflow answers and blog comments.

It’s not the most effective way of finding errors. As you’re learning how to code, you’ll want to gradually get better at coding. This means that you’ll make less mistakes, and that you’ll also know how to better solve them when errors arise – without asking or searching for help!

Quick Tip: It’s always a good idea to clean and rebuild your app project when you run into strange errors. Xcode behaves quirky from time to time, and cleaning the project forces Xcode to do a complete rebuild. Either press Command-Shift-K or Command-Alt-Shift-K to clean your project.

Solution 3: Use Autocomplete Or Quick Help

Coders are lazy. After all, we’re instructing computers to do our work for us. So why type all your code yourself!?

That’s where autocompletion comes in. It’s when you type and Xcode suggests a function or variable in a dropdown list. You can click on an item in the list and Xcode automatically inserts the code for you. You can then use the Tab key to quickly type in function arguments.

In Xcode, autocomplete is enabled by default. If you’ve disabled it, you can always bring it back up by pressing the Esc-key after you’ve typed something.

Use of unresolved identifier

Autocompletion is extremely helpful when it comes to calling, overriding and implementing functions. Functions in Swift can be quite lengthy, and it’s tough to remember them all. With autocompletion you make less mistakes and you save time typing code.

There’s also another side-effect from autocompletion: it uses static analysis to check which functions, properties and variables are available in your code. So when you’re suspecting a particular variable doesn’t exist or isn’t recognized, you can use autocompletion to check if you’re right.

Let’s say you’re working with a detailTextLabel property in a view controller class. You’ve accidentally mistyped it, but you don’t know that yet:

var detailTexLabel:UILabel?

See there’s a t missing?

In viewDidLoad() you’ve typed in the property, but it’s not recognized. You can confirm this by holding the Alt-key and clicking on the property. This will bring up Quick Help, a handy Xcode feature. When Xcode doesn’t recognize the identifier, it’ll show a question mark. Smart!

Use of unresolved identifier

So how do you solve that? Move your cursor to a next line and press the Esc-key. This will show you a dropdown of all available properties, variables and functions. You can filter the input by starting to type something.

In the example above, you could start with typing detai. The list then filters, and you see your property: detailTexLabel. You spot the error and finally understand why the property wasn’t recognized at first.

Quick Tip: Autocompletion can help you debug the scope of variables, properties and functions in your code. You can’t use any variable everywhere in your code – scope determines which variable is available in which part of your code. For instance, variables declared in a function are only available inside that function, whereas properties are available throughout the class. You can get the Use of unresolved identifier error when you’re trying to use a variable that’s unavailable in the scope.

Conclusion

So, now you know! You can use 3 techniques to fix the Use of unresolved identifier error in Xcode:

  • Check your code for typos
  • Google the exact error
  • Use autocompletion and Quick Help to find recognized identifiers

Awesome!

Enjoyed this article? Please share it!

Understanding Use of Unresolved Identifier Error In Xcode Click To Tweet

Reinder de Vries

Reinder de Vries is a professional iOS developer. He teaches app developers how to build their own apps at LearnAppMaking.com. Since 2009 he has developed a few dozen apps for iOS, worked for global brands and lead development at several startups. When he’s not coding, he enjoys strong espresso and traveling.

Comments & Questions

Got a comment or question? Let me know! I read and respond to every one. Thanks!