Reputation: 2948
i'm having difficulty for defining my question in title so if my title is not appropriate then please do correct me ok so thing is that i have an
NSArray
named results
which is fetched from CoreData
and in this array the items have date
key which is of NSDate type . i want to predicate the NSArray
according to the months on Date
key , and am able to do that manually this is how am doing this :
let predicated = NSPredicate(format: "date >= %@ AND date <= %@", start, End)
var filtered = results.filteredArrayUsingPredicate(predicated)
here start
and end
are NSdate
which are first and last date of a given month say am predicating something like "give me the results which are between 1 jan to 31 jan" and its working fine
but i want to predicate my whole result
array in a way so that i don't have to provide the date range , like a function which will take the NSArray
and give me the output with variables of months like
func filterResultArray(NSArray) -> name of months with results {
// here predicate the whole array and add them to the founded months variable
}
print( "\(filterResultArray(resultsArray) )") // should print :
jan = 5 , feb = 18 , march = 5 , may = 87
please tell me what am trying to do using this type of approach is even possible or not and if yes the please give me a little hint or direction about how am gonna do that it'll be so helpful for me
for better understanding please see the example:
example - myArray = [ "1,01,2016", "4,01,2016" ,"28,01,2016", "6,02,2016" "25,02,2016", "29,02,2016" "4,03,2016", "4,04,2016" "10,04,2016", "24,04,2016" "9,05,2016", "29,05,2016" "17,05,2016", "24,05,2016" "12,06,2016", "13,06,2016" ]
now how can i filter this array so that i can get a results something like :
January = 3
feb = 3
march = 1
april = 3
may = 4
june = 2
Upvotes: 0
Views: 372
Reputation: 5554
The solution that @Joshua has shown you looks very elegant - assuming you are only interested in the month - meaning March 2016 counts as the same thing as March 2015 - then when you're parsing your data into myArray
, just create an array of myMonths
and strip out the middle data element to give you the month.
It's going to look something like this -
let dateComponents = resultDate.componentsSeparatedByString(",")
myMonths.append(dateComponents[1])
if you do need to include the year in the count, then just append the strings so that you get "201603"
or "032016"
Upvotes: 1