|
Hello, I have multiple entities of a type which I would like to extract all of the elements of a certain field into an array and then perform a math operation onto it. An example is if I had multiple homes each with a field 'price' and I would like to determine the max price. I expect either a fetch or or calling allObjects and converting the target field to an array operation followed by calling .max() on the array. Thank you |
|
You can use Some sample code (from Group by, Count and Sum in CoreData | Cocoanetics):
Another example, from objc.io's book Core Data (pp82-83):
Hopefully these can get you started. And maybe someone else on here has an example from their own code they can share. |
|
thanks for posting, I believe the first example is exactly what I am looking for. Will try it out |
|
hmm, I must be missing something. let's take a simple department, employee example, in which I would like to find the sum of employee computers. The following expected code does not work for me. What am I doing wrong? results is always invalid. Thank you
Edit: put source in code block for clean formatting |
|
First things first... When posting code to these forums, place three backticks This makes it far easier to read and also makes it easier for other posters to copy/paste the code in order to test solutions and such. Doing this will ensure that you end up with something like this:
instead of this: let keypathExp1 = NSExpression(forKeyPath: "computer") let expression = NSExpression(forFunction: "sum:", arguments: [keypathExp1]) let sumDesc = NSExpressionDescription() sumDesc.expression = expression sumDesc.name = "sum" sumDesc.expressionResultType = .integer64AttributeType let request = NSFetchRequest(entityName: "department") request.returnsObjectsAsFaults = false request.propertiesToFetch = [sumDesc] request.resultType = .dictionaryResultType let fetch: NSFetchRequest = department.fetchRequest() do{ let results = try? fetch.execute() print(results ?? "invalid?") // results is now an array } catch { NSLog("Error fetching entity: %@", error.localizedDescription) } |
|
|
|
Sorry it's taken me so long to respond; real life got in the way for a while. Anyway, check out this simple sample project I uploaded to github: roosterboy/CoreDataFunctions Hopefully, this will give you a good idea how to do what you are looking to do. |
|
thanks so much for the reply @roosterboy. Taking a look at this solution now. Will reply back |
SPONSORED From March 20th to 26th, you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.