2
votes

Could help me guys with the following three errors in SwiftCoreDataHelper.swift. I am using xcode 7 but its seems that the code for older versions of xcode. thanks in advance.

1 . . .

line error: let items: NSArray = managedObjectContext.executeFetchRequest(<#T##request: NSFetchRequest##NSFetchRequest#>) error: Call can throw, but it is not marked with 'try' and the error is not handled

 class func fetchEntities(className:NSString, withPredicate predicate:NSPredicate?, managedObjectContext:NSManagedObjectContext)->NSArray{
    let fetchRequest:NSFetchRequest = NSFetchRequest()
    let entetyDescription:NSEntityDescription = NSEntityDescription.entityForName(className as String, inManagedObjectContext: managedObjectContext)!

    fetchRequest.entity = entetyDescription
    if predicate != nil {
        fetchRequest.predicate = predicate!
    }

    fetchRequest.returnsObjectsAsFaults = false
    let items: NSArray = managedObjectContext.executeFetchRequest(<#T##request: NSFetchRequest##NSFetchRequest#>)

    return items
}

2 . . .

line error: if managedObjectContext.save(nil) {

error: Nil is not compatible with expected argument type '()'

class func saveManagedObjectContext(managedObjectContext:NSManagedObjectContext)->Bool{
    if managedObjectContext.save(nil) {
        return true
    }
    else {
        return false
    }
}

3 . . .

line error: if storeCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error:&error ) != nil {

error: Contextual type '[NSObject : AnyObject]' cannot be used with array literal

 if storeCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error:&error ) != nil {

    if error != nil {
        print(error!.localizedDescription)
        abort()
    }
 }
1
Sorry. Newbie here. By the thanks for the correctionJoriel Flynn Mazo
Say clearly which line of each code generates the compiler error.matt
Both "Call can throw, but it is not marked with 'try' and the error is not handled" and "Nil is not compatible with expected argument type '()'" indicate that your code is not adapted to the new error handling in Swift 2. You'll find many Q&A related to that topic, and in particular to the first error message. For example: Swift 2 ( executeFetchRequest ) : error handling. I strongly recommend that you read about error handling in the current Swift documentation.Martin R

1 Answers

0
votes
  1. Read about do try throw and catch. If a call throws you should enclose it in a do block and handle the error with catch.
  2. Remove the nil. No argument is expected. You may then have to handle thrown errors.
  3. Not quite sure what the error is here but again this needs converting to Swift 2 error handling and should probably look something like this. Note the error argument has become a thrown error. This is untested no promises that it is absolutely correct.

    do {
      let coordinator = try storeCoordinator.addPersistentStoreWithType(
            NSSQLiteStoreType, 
            configuration: nil, 
            URL: url, options: nil)
    } catch {
      print(error)
      abort()
    }