I am trying to write a method:
- (NSDictionary *)wordFrequencyFromString:(NSString *)string {}
where the dictionary returned will have the words and how often they were used in the string provided. Unfortunately, I can’t seem to find a way to iterate through words in a string to analyze each one – only each character which seems like a bit more work than necessary. Any suggestions?
NSString has
-enumerateSubstringsInRange:method which allows to enumerate all words directly, letting standard api to do all necessary stuff to define word boundaries etc:In the enumeration block you can use either NSDictionary with words as keys and NSNumber as their counts, or use NSCountedSet that provides required functionality for counts.