• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSSpellChecker and checkSpellingOfString problems
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSSpellChecker and checkSpellingOfString problems


  • Subject: NSSpellChecker and checkSpellingOfString problems
  • From: Keith Blount <email@hidden>
  • Date: Thu, 3 Dec 2009 08:55:20 -0800 (PST)

Hello,

I have an NSTextView subclass that provides a custom contextual menu by overriding -menuForEvent:. Because I override this, I have to provide any menu items I want to retain from the original menu myself. Mostly, that's not a problem, but I want to keep the spell checking options at the top of the menu as well. I thought I had this covered. This is what I'm doing:


// Is there a selection?
if (selRange.length > 0 && selRange.location < [text length])
{
	// If so, check to see if the selected range constitutes a misspelled word.
	NSSpellChecker *spellChecker = [NSSpellChecker sharedSpellChecker];
	NSRange misspelledRange = [spellChecker checkSpellingOfString:[[text string] substringWithRange:selRange] startingAt:0];

	// Detected misspelled word?
	if (misspelledRange.length == selRange.length)
	{
		// Get suggestions.
		NSArray *suggestions = [spellChecker guessesForWord:[[text string] substringWithRange:selRange]];

		// Are there any suggestions?
		if ([suggestions count] > 0)
		{
			// If so, add them to the menu with an appropriate action.
		}
		else
		{
			// Otherwise insert the "No Guesses Found" item.
		}
}

I thought all of this was working fine. However, a user has just pointed out to me that it doesn't work for all misspellings... Which is very strange. The problem comes down to NSSpellChecker's -checkSpellingOfString:. This returns an NSNotFound range for certain misspellings.

For instance:

Try typing "accade" into TextEdit (I'm assuming English as the language here, of course). It is underlined in red, and ctrl-clicking on it brings up a list of suggestions. So the system recognises it as a misspelling, a word that it doesn't know.

Now try this in any test app:

NSRange range = [[NSSpellChecker sharedSpellChecker] checkSpellingOfString:@"accade" startingAt:0];
NSLog (@"NSStringFromRange(range));

range will be (NSNotFound,0).

I don't understand why, though. Why isn't NSSpellChecker returning this as a misspelling? Am I missing something obvious? Is there a better way to insert the spelling suggestions at the top of the menu?

Thanks and all the best,
Keith



_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: NSSpellChecker and checkSpellingOfString problems
      • From: Knut Lorenzen <email@hidden>
  • Prev by Date: Re: Finding a word from a character position
  • Next by Date: Re: Application crashing on iPod Touch, not on iPhone
  • Previous by thread: Re: Reboot? Slow First Run
  • Next by thread: Re: NSSpellChecker and checkSpellingOfString problems
  • Index(es):
    • Date
    • Thread