Find SUM of an array of managed objects - objective-c

I have a big problem with taking a simple Total Cost from an array!
I have an entity called currentCost, it contains two attributes: costAmount and costDesc. I bind it to an array controller and I have no problem when I add and remove items to and from this entity. The problem is to get the Total Cost from the costAmount attributes and show it in a text field.
I think I have to fetch all costAmounts to another array and take the total of them, but it seems I don't know how to do this! What is the best approach ?
Thank you in advance.

Bind the text field to the array controller and specify #sum.costAmount as the key path.

Related

Liquid - Filter Array Of Products Using Properties

I would like to filter an array of products using values from the properties field. Specifically so in the cart with line_items.
I am currently trying to use the where filter, to only include products with the property of "group-id". Ideally, this would be as easy as:
{% assign filtered_items = items | where: 'properties.group-id' %}
But from trying this, and searching around in the documentation and online, it doesn't look like others were able to get it to work.
Example 1
Example 2
Jekyll looks to have a where_exp filter which would be able to solve this nested issue if it were a part of Shopify Liquid.
I understand this can be implemented using a for loop with conditional checks, but I am curious about the possibility this method.
I wasn't able to use the where filter as I was hoping, but ended up solving the issue anyway.
In my use case, I needed to create sub-arrays based on a common property. This common property always showed up contiguously across items in the original array.
This made the situation a little easier. I ended up iterating through the original array, kept track of the relevant indices, and took slices of it using slice to create the sub-arrays.

SRSS: Dynamic amount of subreports in a report

it might be possible I'm searching for the wrong keywords, but so far I couldn't find anything useful.
My problem is quite simple: At the moment I get a list of individual Ids through a report parameter, I pass them to a procedure and show the results.
The new request is like this: Instead of showing the list for all individuals at once, there should be a list for each individual id.
Since I'm quite a beginner in srss, I thought the easiest approach would be the best: Create a subreport, copy the shown list, and create a subreport per individual id.
The amount of this IDs is dynamic, so I have to create a dynamic amount of subreports.
Funny enought, this doesnt seem to be possible. This http://forums.asp.net/t/1397645.aspx url doesnt show exactly the problem, but it shows the limit of the subreports.
I even ran trough the whole msdn pages starting http://technet.microsoft.com/en-us/library/dd220581.aspx but I couldnt find anything there.
So is there a possibility, to create a loop like:
For each Individual ID in Individual IDs, create a subreport and pass ONE ID to this?
Or is there another approach I should use to make this work?
I tried to create a 'Fake'-Dataset with no sql query but just for iterating the id list, but it seems the dataset needs a data-source...
As usual, thanks so far for all answers!
Matthias Müller
Or is there another approach I should use to make this work?
You didn't provide much detail about what sort of information needs to be included in the subreport, but assuming it's a small amount of data (say, showing a personnel record), and not a huge amount (such as a persons sales for the last year), a List might be the way to go.
I tried to create a 'Fake'-Dataset with no sql query but just for iterating the id list, but it seems the dataset needs a data-source...
All datasets require a data source, though if you're merely hard-coding some fake return data, any data source will do, even a local SQL instance with nothing in it.

IOS tableviews working with dictionaries as models

i am starting to think through an example program i want to try. the premis is simple in that it lists me a large list of names with their gender.
for now i started to have a Person Class which has name and gender as properties. after that i build up a dictionary with the alphabetic letters as keys and an array of persons underneath each other. that works fine as in the table methods i can easily get the persons for each alphabet letter.
now i want to implement that i can switch of male or female.
the problem i'm running into is that all tableview controller methods would suddenly get much more complex. as an instance the numberOfRowsInSection method. before i just got the array for the letter key and returned the count. now i would have to parse through everything and count up the male Persons or female.
i feel like there is maybe a better way to approach this. but i'm not certain.
any ideas ? thanks in advance
You either have to do it dynamically as you suggest which could become really slow if there are a lot of people in the list.
The other approach is to separate things into 3 arrays: one that contains everyone, one that contains males, and one that contains females.
Then you have a pointer that references one of the 3 arrays and you change this pointer when the user changes their preferences.

Using NSFetchedResultController with no sorting

1) I wanna display my search results in the same order returned by the web Service, but it seems 'An instance of NSFetchedResultsController requires a fetch request with sort descriptors'.
2) I still wanna use a NSFetchedResultsController because I allow user to sort by date, etc, but if no sorting is chosen I want to display them in the exact order I got them.
3) Another thing, depending on the search, the items might have different priority. Since I store every item, I cannot just create a priority for each since it won't apply to every case.
Thanks in advance
Lucas,
If you want to enforce an order, then you need an attribute to sort against. I suggest you add a serial number to your model and bump it as you insert items.
Andrew

Distance group NSFetchedResultsController sections

I'm using Core Data for a tableview. My data is ordered by the distance from your current location. What I'd like is to have a new section for items with 5, 10 and 20 miles.
My distance value is stored in the data store as an NSInteger and I get it out using a NSNumber in my object model.
I've done a bit of searching around and found that I need to use the sectionNameKeyPath attribute to make the data sectioned.
My problem is that I don't know the best way to group the data. During my searches I came across either a transient property or using a category of NSNumber to work out which section the item should be in.
Are either of these methods the best way of getting my end result, if so, can anyone provide any details on how to implement it?
Thanks.
You should add a derived attribute to the object (it's not strictly needed in the model), let's name it range. I'd make it a read only property, you can cache the value or not.
When it's 0-5, return 5, 5.x - 10 return 10, etc.
Then set range as your sectionNameKeyPath.
If you want a highly customized section behavior, you need to subclass NSFetchedResultsController to give you the behavior you want. See the NSFetchedResultsController class docs for details.
You will need to subclass in this case because your not looking at a single ordered attribute like the alphabetized first letter of a string attribute but rather a range in which each attribute falls into.
This is a cleaner solution than altering the data model because you can use it to display the data many different ways in many different tableviews without having to muddy up your data model.