0

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
remy boys
  • 2,928
  • 5
  • 36
  • 65
  • 1
    create a new `NSCountedSet` and then for every item in results get date, from it get the month (`NSCalendar` operations) and put it into the set. In the end your counted set will contain exactly what you want. – Sulthan Mar 02 '16 at 19:51
  • @Sulthan thanks for responding man , okay let me try what you just said – remy boys Mar 02 '16 at 19:55
  • 1
    Please see http://stackoverflow.com/questions/30545518/how-to-count-occurrences-of-an-element-in-a-swift-array in case you're interested in a solution that doesn't involve NSCountedSet. – Joshua Kaden Mar 02 '16 at 20:18
  • @JoshuaKaden yeah your suggested link seems better option , but how am gonna count (with an range of months ) ?? in an NSArray ? – remy boys Mar 02 '16 at 20:22
  • because in my array values are like `["1,01,2016","21,01,2016","04,02,2016"]` they are not as similar as the answer's item – remy boys Mar 02 '16 at 20:24

1 Answers1

1

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"

Russell
  • 5,436
  • 2
  • 19
  • 27