How do fix the "Cannot find 'NSFetchRequest' in scope error

Mark Reggiardo picture Mark Reggiardo · Jun 26, 2020 · Viewed 22.1k times · Source

I have tried to make an NSFetchRequest in many different ways and each time I get this error:

"Cannot find type 'NSFetchRequest' in scope"

Here are the specific ways I have tried:

let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Task")

let fetchRequest: NSFetchRequest<Task> = Task.NSFetchRequest()

let fetchRequest = Task.fetchRequest() as! NSFetchRequest<Task>

Any idea why this is giving me this error? I know it's unlikely;iklely but I'm on the Xcode 12 beta, could this be a bug with Xcode? I followed a tutorial as I am just learning SwiftUI (and Swift in general) and so it would seem to me this code should work. I have looked in many places to try to find an answer to this so if it's obvious, I'm sorry I missed it.

Answer

Frederik Brammer picture Frederik Brammer · Aug 5, 2020

Cannot find * in scope

There are several reasons why this error might appear in your code.

  • #1 You didn't import the framework.
  • #2 If you are using cocoapods, the pod might not be installed.
  • #3 You opened the .xcodeproj file instead of the .xcworkspace file (If you are using cocoapods).
  • #4 The variable does not exist.

Solving error #1

If the first option is the case, you need to look up which framework is used by the object/method. If it is an external framework (not shipped with Xcode), it might be a good idea to take a look at the site you got the code from / contact the developer if you got it from GitHub.

Solving error #2

To install cocoapods in your Xcode project, you need to open a new Terminal window. There, you need to navigate to your Xcode project. To do so, just type in cd /path/to/your/xcodeproject. Then you can type pod init. If there is an error saying -bash: pod: command not found, then cocoapods is not installed. To install it, just type sudo gem install cocoapods. Then pod init should work just fine. This command creates a file called Podfile. Open it and add pod 'name-of-your-pod'. Save and close the file. Back in the terminal, type pod install. This will now take some time depending on the size of the pods. When it is done, there should be a .xcworkspace file. From now on, you need to open that instead of the standard .xcodeproj file.

Solving error #3

If you are using cocoapods, those pods are only used by Xcode when you open the .xcworkspace file - just open it, and you're good to go!

Solving error #4

You forgot to create the variable you're trying to access. To create it, just type var myVar = "myString" if you want to change it or replace var with let if it will never be changed. You also need to replace "myString" with your desired value.

As you can see, this error can be caused by many mistakes you made in code / while importing, but it is crucial to understand the reason for this error as a beginner as you might come across it more often than you think! Also, some of the explanations contain steps explained for absolute beginners, but it should be understandable by everyone. I hope that helps anybody!