user6718553 user6718553 - 3 months ago 50
Swift Question

use of unresolved identifier 'entityDescription'

An error use of unresolved identifier 'entityDescription' is coming in my code. Even after a lot of failed attempts am unable to resolve it.

import UIKit
import CoreData

class SwiftCoreDataHelper: NSObject {

class func directoryForDatabaseFilename()->NSString{
return NSHomeDirectory().stringByAppendingString("/Library/Private Documents")
}


class func databaseFilename()->NSString{
return "database.sqlite";
}


class func managedObjectContext()->NSManagedObjectContext{


do { try NSFileManager.defaultManager().createDirectoryAtPath(SwiftCoreDataHelper.directoryForDatabaseFilename() as String, withIntermediateDirectories: true, attributes: nil) }
catch {
print("Error Creating Directory for DB")
}
// NSFileManager.defaultManager().createDirectoryAtPath(SwiftCoreDataHelper.directoryForDatabaseFilename(), withIntermediateDirectories: true, attributes: nil, error: &error)

let path:NSString = "\(SwiftCoreDataHelper.directoryForDatabaseFilename()) + \(SwiftCoreDataHelper.databaseFilename())"

let url:NSURL = NSURL(fileURLWithPath: path as String)

let managedModel:NSManagedObjectModel = NSManagedObjectModel.mergedModelFromBundles(nil)!

let storeCoordinator:NSPersistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: managedModel)

do {
try storeCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
}
catch {
print("Error: \(error)")
}

let managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType)

managedObjectContext.persistentStoreCoordinator = storeCoordinator

return managedObjectContext


}

class func insertManagedObject(className:NSString, managedObjectConect:NSManagedObjectContext)->AnyObject{

let managedObject:NSManagedObject = NSEntityDescription.insertNewObjectForEntityForName(className as String, inManagedObjectContext: managedObjectConect) as NSManagedObject

return managedObject

}

class func saveManagedObjectContext(managedObjectContext:NSManagedObjectContext)->Bool{
do {
try managedObjectContext.save()
return true
} catch {
return false
}
}


class func fetchEntities(className:NSString, withPredicate predicate:NSPredicate?, managedObjectContext:NSManagedObjectContext)->NSArray{
let fetchRequest:NSFetchRequest = NSFetchRequest()


guard let entityDescription = NSEntityDescription.entityForName(className as String, inManagedObjectContext: managedObjectContext) else {
//handle error here
return
}

fetchRequest.entity = entityDescription
if let predicate = predicate {
fetchRequest.predicate = predicate
}

fetchRequest.returnsObjectsAsFaults = false
var items = NSArray()
do { items = try managedObjectContext .executeFetchRequest(fetchRequest)
} catch {
print("Fetch Request Failed")
}
return items}}


The error is coming in the code-

fetchRequest.entity = entityDescription
if (predicate != nil){
fetchRequest.predicate = predicate!
}


I am not understanding where am going wrong.
Any help appreciated. Thanks in advance

Answer

In an if let statement, the new variable name is bound only for the duration of the if let scope. You can't use it afterwards. What you're after is the guard statement:

guard let entityDescription = NSEntityDescription.entityForName(className as String, inManagedObjectContext: managedObjectContext) else {
    //handle error here
    return
}

fetchRequest.entity = entityDescription
if let predicate = predicate {
    fetchRequest.predicate = predicate
}