0

Since migrating from 2.2 to 2.3 I now get an the following error:

"Extra Argument in call" in the following code:

class func objectCountForEntity (entityName:String, context:NSManagedObjectContext) -> Int {

let request = NSFetchRequest(entityName: entityName)
var error:NSError?
let count = context.countForFetchRequest(request, error: &error)

if let _error = error {
  print("\(#function) Error: \(_error.localizedDescription)")
} else {
  print("There are \(count) \(entityName) object(s) in \(context)")
}
return count
}

Could any one advise how I obtain an entity count in swift 2.3 since countForFetchRequest no longer functions in the way it did in swift 2.2

Nirav D
  • 71,513
  • 12
  • 161
  • 183
sapjjr
  • 147
  • 1
  • 6
  • Actually for `countForFetchRequest`, it changed between 2.2 and 2.3. I have updated http://stackoverflow.com/questions/34652618/countforfetchrequest-in-swift-2-0 accordingly. – Martin R Sep 14 '16 at 14:55

1 Answers1

1

Swift 3.1

This is work for me.

class func objectCountForEntity (entityName:String, context:NSManagedObjectContext) -> Int {

let request = NSFetchRequest(entityName: entityName)
var error:NSError?
let count = try! context.count(for: request)

if let _error = error {
    print("\(#function) Error: \(_error.localizedDescription)")
} else {
    print("There are \(count) \(entityName) object(s) in \(context)")
}
return count
}
Durul Dalkanat
  • 7,266
  • 4
  • 35
  • 36