Back to blog

Understanding The “Use of Unresolved Identifier” Error in Xcode


Aasif Khan
By Aasif Khan | Last Updated on August 7th, 2023 6:43 am | 5-min read

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 tutorial 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.

What Does “Unresolved Identifier” Mean?

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 is no different.

In the error message “identifier” refers to a framework, a class, a function, a property or a variable. Differently said, an identifier is something that can be identified by name, such as a function.

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

let dog = Dog(name: “Dogmeat”)
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.

“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!”

1. Check Your Code For Typos

As you know, writing programming code is exact and unforgiving. When you make just one typing mistake, you’ll get an error. Swift code is case-sensitive too, so myHumpyCamel isn’t the same as myhumpycamel.

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 = “Hashtag cred selvage locavore prism twee”

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 uppercase 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, especially if you don’t indent your code.
  • 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…

2. Look Up The Exact Error Message

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 search Google for the exact error you’re seeing in Xcode:

Use of unresolved identifier ‘Alamofire’

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 come 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 Stack Overflow 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 + Option + Shift + K to clean your project.

3. Use Auto-Complete 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 auto-completion 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.

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 Option 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!

So how do you solve that? Move your cursor to a next line and press the Esc. 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:

  1. Check your code for typos
  2. Search the exact error message
  3. Use autocompletion and Quick Help to find recognized identifiers


Aasif Khan

Head of SEO at Appy Pie

App Builder

Most Popular Posts