gavsta707 gavsta707 - 5 months ago 22
iOS Question

Difficulty figuring out how to fetch an NSManagedObject subclass entity from CoreData


I'll admit that i am mainly guessing and trying to piece together things i find online to get the job done here. This job is very simple: I am trying to retrieve a single attribute from the entity "CrawlerOne" and set it to a UILabel.


let key = "CrawlerDistance"
let request = NSFetchRequest<CrawlerOne>(entityName: "CrawlerOne")
request.returnsObjectsAsFaults = false;
let resultPredicate = Predicate(format: "formUUID = %@" , key)
var compound = CompoundPredicate.init(andPredicateWithSubpredicates: [resultPredicate])
var results:NSArray = try context.executeFetchRequest(request, error:nil)

Like i said, i undoubtably know this is wrong. I am trying to find an entity with the name "CrawlerDistance" using NSPredicate. I get an error message on the last like that says "Use of unresolved identifier 'context'." I am very lost and struggling here and would appreciate some guidance. I would really like to know if i am heading in the right direction with my thought process and/or what i need to get past this error/fix any issues.

This is written in swift 3 for excode beta 8.0


This is my code for saving the object i am trying to retrieve:

let appDel:AppDelegate = (UIApplication.shared().delegate as! AppDelegate)
let context:NSManagedObjectContext = appDel.managedObjectContext
let entity1 = NSEntityDescription.insertNewObject(forEntityName: "CrawlerOne", into:context) as NSManagedObject as! CrawlerOne
entity1.crawlerDistance = distance.text

This the CrawlerOne+CoreDataProperties.swift file

import Foundation
import CoreData

extension CrawlerOne {

@nonobjc class func fetchRequest() -> NSFetchRequest<CrawlerOne> {
return NSFetchRequest<CrawlerOne>(entityName: "CrawlerOne");

@NSManaged var crawlerAbrasion: String?
@NSManaged var crawlerDistance: String?



Just to give you a code sample to compere to, without exactly explaining all the places that are wrong, and obviously i can't test this so some things may still be problematic...:

let targetValue = "CrawlerDistance"
let request = NSFetchRequest<CrawlerOne>(entityName: "CrawlerOne")
request.predicate = Predicate(format: "crawlerDistance = %@" , targetValue)

let appDel:AppDelegate = (UIApplication.shared().delegate as! AppDelegate)
let context:NSManagedObjectContext = appDel.managedObjectContext

var results:NSArray = try context.executeFetchRequest(request)