Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 845
Mark Reggiardo Avatar asked Jun 26 '20 21:06

Mark Reggiardo


2 Answers

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
  5. The file was not added to target

#1 You didn't import the framework.

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.

#2 If you are using cocoapods, the pod might not be installed

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.

#3 You opened the .xcodeproj file instead of the .xcworkspace file (If you are using cocoapods)

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!

#4 The variable does not exist

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. Another reason could be that you defined the variable you are trying to access inside of a closure, meaning that it cannot be accessed from outside. To avoid that, define your variable before the closure.

#5 The file was not added to target

In order for Xcode to know what to include when building your app, it uses so-called "targets". If your file is not added to any target, the compiler will fail to find the file. To add your file to a target, select the file, and on the pane on the right, select Target Membership -> (YourAppName). Rebuilding the project should fix the issue (thanks to @Sravan for pointing this out).

like image 135
Unterbelichtet Avatar answered Oct 10 '22 01:10

Unterbelichtet


Had this same error for about an hour now,

import CoreData

You would be missing this line if it says its out of scope.

like image 38
Eric Lancaster Avatar answered Oct 10 '22 02:10

Eric Lancaster