In my swift code below I am trying to fetch all of my names in core data to print. I am getting a compile error on context saying it can't be found in scope. I have attached a photo as well so you can see what is in my core data.
@objc func pressRight(){
var locations = [Place]() // Where Locations = your NSManaged Class
var fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "name")
locations = context.executeFetchRequest(fetchRequest, error: nil) as [Place]
// Then you can use your properties.
for location in locations {
print(location.name)
}
}

This isn't a Core Data problem, it's a Swift problem. All variables and properties in Swift have to be declared before you can use them. For example you can't just write
Unless somewhere you already have something like
In your code you're using something called
contextthat was never declared. Swift doesn't know what it is, which is what it's telling you.Since you're using Core Data it looks like you want
contextto be an instance ofNSManagedObjectContext. If your app already has anNSPersistentContainersomewhere, you can get a context from it, using eitherviewContextornewBackgroundContext(which one depends on exactly how you're using it; here it's probablyviewContext).If you don't already have an
NSPersistentContainersomewhere in your app, you may want to read up a little on how to use Core Data. You can't just declare the context; it needs to be configured correctly with the container before use.