Re: Word Count Problem
Re: Word Count Problem
- Subject: Re: Word Count Problem
- From: Matt Ball <email@hidden>
- Date: Mon, 3 May 2004 23:04:49 -0400
Wait, never mind. I fixed it. For anyone else who may have this problem
in the future, here is my revised code:
- (IBAction)countWords:(id)sender
{
// count the words, and log to the command line
int count = 0;
count = [[NSSpellChecker sharedSpellChecker]
countWordsInString:[textView string] language:@""];
NSString* words = @" words.";
if(count == -1)
count = 0;
else if(count == 1)
words = @" word.";
[wordCountLabel setStringValue:[[@"Your document contains "
stringByAppendingString:[NSString stringWithFormat:@"%i", count]]
stringByAppendingString:words]];
[NSApp beginSheet: wordCountSheet
modalForWindow: mainWindow
modalDelegate: self
didEndSelector: nil
contextInfo: nil];
[NSApp endSheet:wordCountSheet];
}
-- Matt Ball
On May 3, 2004, at 9:36 PM, Matt Ball wrote:
>
That makes sense. Any idea how to fix it? Or even another method to do
>
a word count? Thanks
>
-- Matt Ball
>
On May 3, 2004, at 7:16 PM, cricket wrote:
>
>
> Minor correction to earlier post:
>
>
>
> On May 3, 2004, at 3:55 PM, Matt Ball wrote:
>
>
>
>> while(range.location != NSNotFound)
>
>> {
>
>> range = [[NSSpellChecker sharedSpellChecker]
>
>> checkSpellingOfString:textDocument startingAt:0 language:@"" wrap:YES
>
>> inSpellDocumentWithTag:0 wordCount:&count];
>
>> wordCount += count;
>
>> }
>
>
>
> This block here will end up in an infinite loop if the document
>
> contains misspelled words -- range.location will end up being
>
> something other than NSNotFound, then the loop will repeat, starting
>
> at the beginning of the string again.
>
>
>
> If you have an application hanging, Sampler is a good tool to use to
>
> troubleshoot it. Also, stepping through your block of code in XCode
>
> or gdb would quickly reveal that you're stuck in the while loop
>
> forever.
>
>
>
> - cricket
>
>
>
>
>
>> It works fine for documents that have not set off a red flag in the
>
>> spellchecker. As soon as the document contains a word that the
>
>> spellchecker doesn't know, the word count stops working. The app
>
>> freezes when I try to do a word count. Can anyone offer any insights
>
>> as
>
>> to why this is happening?
>
> _______________________________________________
>
> cocoa-dev mailing list | email@hidden
>
> Help/Unsubscribe/Archives:
>
> http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
> Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.