0

I am presently making my first steps using Core Data, with Swift. Even though I have quite a bit of experience with Core Data, I have close to none with Swift.

Here is what I want to do: make a function which is going to compute the number of records in an entity, from the name of the entity.

Something like this:

func countEntity (name:String) -> Int {
    var theNumberOfRecords:Int
    let fetchRequest = NSFetchRequest(entityName:name)
    // What I have tried at this point only gives error messages …..
    return theNumberOfRecords
}

It should not be rocket science. But I have not been able to make something work after various trial and errors. How should I write the necessary code? I presume it should be no more that a few lines. The one I have in Objective C is 6 lines.

1
  • If you want to write a lot fewer lines of code, try using github.com/Prosumma/CoreDataQueryInterface. Your query would be something like managedObjectContext.from(Entity).filter({$0.name == name}).count() and that's it. Commented Jun 6, 2015 at 1:57

1 Answer 1

1

You get the number of objects a fetch request would have returned with countForFetchRequest():

func countEntity (name:String, context : NSManagedObjectContext) -> Int {
    let fetchRequest = NSFetchRequest(entityName:name)
    var error : NSError?
    let theNumberOfRecords = context.countForFetchRequest(fetchRequest, error: &error)
    if theNumberOfRecords == NSNotFound {
        println("Error: \(error?.localizedDescription)")
    }
    return theNumberOfRecords
}

In the case of an error, countForFetchRequest() returns NSNotFound and you have to decide how to handle that situation.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. When writing what you suggest. I first get a complaint asking for a ! after context After fixing this, I have no error. But then, when I try to call this function (which I have placed in AppDelegate) from the RootViewController I get an error saying: Cannot invoke 'countEntity' with an argument list of type '(String)' I don’t quite understand why, but I suppose I must be making some beginner mistake. Am I not missing the equivalent of "#import AppDelegate.h" in objective C, if that ever exists?
@Michel: I changed the signature of the function so that it must be called as countEntity("YourEntityName", context). Does the RootViewController have a pointer to the managed object context? How did you do it in Objective-C? Perhaps something like let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext! is what you are looking for, compare stackoverflow.com/a/24046979/1187415.
I saw that, but I do not think this is the issue, because I kept the old signature. The RootViewController not having a pointer to the managed object context, I use the local one already inside AppDelegate. This is what I always did in ObjC. But how can RootViewController know about the proper signature for a function somewhere else? Since there is no "#import AppDelegate.h" like thing? I am missing some detail.
Following what is in the link you just mentioned. I was able to make it work, with both your signature and mine. It seems like I need a bit of practice in Swift:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.