Just updated to Xcode 7.0 from Xcode 6.4. In my project I am getting an error now which I try to solve the whole night and did not get it.
The error message is: Initializer for conditional binding must have optional type not 'nsmanagedobjectcontext'
The error is coming twice in lines if let managedObjectContext = self.managedObjectContext { in following code
func preloadData () {
// Retrieve data from the source file
if let contentsOfURL = NSBundle.mainBundle().URLForResource("listofdata", withExtension: "csv") {
// Remove all the items before preloading
removeData()
var error:NSError?
if let items = parseCSV(contentsOfURL, encoding: NSUTF8StringEncoding, error: &error) {
// Preload the items
if let managedObjectContext = self.managedObjectContext {
for item in items {
let listOfItem = NSEntityDescription.insertNewObjectForEntityForName("ListOfItem", inManagedObjectContext: managedObjectContext) as! ListOfItem
listOfItem.name = item.name
listOfItem.address = item.address
listOfItem.phone = item.phone
if managedObjectContext.save(&error) != true {
print("insert error: \(error!.localizedDescription)")
}
}
}
}
}
}
func removeData () {
// Remove the existing items
if let managedObjectContext = self.managedObjectContext {
let fetchRequest = NSFetchRequest(entityName: "ListOfItem")
var e: NSError?
let listOfItems = managedObjectContext.executeFetchRequest(fetchRequest, error: &e) as! [ListOfItem]
if e != nil {
print("Failed to retrieve record: \(e!.localizedDescription)")
} else {
for listOfItem in listOfItems {
managedObjectContext.deleteObject(listOfItem)
}
}
}
}
Appreciate all help! Thanks!
Update:
The updated code looks like this, but still have these two errors in the first function preloadData:
- Missing argument for parameter 'error' in call
Initializer for conditional binding must have Optional type, not 'NSManagedObjectContext'
func preloadData () { // Retrieve data from the source file if let contentsOfURL = NSBundle.mainBundle().URLForResource("listofdata", withExtension: "csv") { // Remove all the menu items before preloading removeData() do { let items = try parseCSV(contentsOfURL, encoding: NSUTF8StringEncoding) // Preload the items if let managedObjectContext = self.managedObjectContext { for item in items { let listOfItem = NSEntityDescription.insertNewObjectForEntityForName("ListOfItem", inManagedObjectContext: managedObjectContext) as! ListOfItem listOfItem.name = item.name listOfItem.address = item.address listOfItem.phone = item.phone if managedObjectContext.save() != true { print("insert error: \(error.localizedDescription)") } } } } catch let error as NSError { print("insert error: \(error.localizedDescription)") } }
This function shows no errors
func removeData () {
// Remove the existing items
let fetchRequest = NSFetchRequest(entityName: "ListOfItem")
do {
let listOfItems = try self.managedObjectContext.executeFetchRequest(fetchRequest) as! [ListOfItem]
for listOfItem in listOfItems {
self.managedObjectContext.deleteObject(listOfItem)
}
}
catch let error as NSError {
print("Failed to retrieve record: \(error.localizedDescription)")
}
Can some help? Thanks!