I am starting to learn how to code a mac application using Objective C. Right now I have a TextView filled up with text. Is there any way I can get the number of instances a specific word appearing in that textview.
I have read up on NSTextFinder Class but I can’t set my TextView as the client of the NSTextFinder and there is no sample code to help me along. Any help is appreciated
I’m assuming that all you need to do is provide a standard Cocoa find bar capability for
NSTextViewin which case it’s justself.textView.usesFindBar = YESfor theNSTextViewin question. When the users presses COMMAND + FPerhaps you’re looking for a solution that doesn’t use the find UI – but then you wouldn’t be using
NSTextFinderwhose function is to serve as the find UI’s controller class… If you’re looking for a non-UI-based search and count mechanism then edit your question and we can try to respond.EDIT:
If you just want to count the occurrences of a word in the
NSTextView‘s thenNSScanneris a solution. See example ofNSScanneras word counter in action:This prints out: