4

How do you use UILexicon in Objective-C? I find the documentation Apple provides is extremely unhelpful.

What does it do? Does it return a dictionary or proper spellings of words? Or do I provide a word like "hellllo" and it matches it with the proper spelling "Hello" and returns that as a string?

Any help would be appreciated.

requestSupplementaryLexiconWithCompletion:


Here's my error report, but obviously I'll have errors because I'm completely guessing how to use the function, no clue what goes inside the block statement (because the docs (at the time) don't say! (Beta 4 docs)) Hahahah! enter image description here

Emil
  • 7,220
  • 17
  • 76
  • 135
Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
  • 6
    Someone with your rep should know that such a question should include details about the crash and error as well as relevant code. – rmaddy Jul 23 '14 at 16:51
  • 2
    It's probably unhelpful because it's *pre-release*. Don't expect that it'll be the final iteration of that documentation. – esqew Jul 23 '14 at 16:53
  • @rmaddy There is no actually specific error, the app can't even run because I don't know how to use the function at all. It would crash the same reason `int j = 0.5;` would crash. I'm not properly using the function. – Albert Renshaw Jul 23 '14 at 17:20
  • @rmaddy Added my error report. – Albert Renshaw Jul 23 '14 at 17:27
  • 2
    The `requestSupplementaryLexiconWithCompletion` method is from the `UIInputViewController` class, not `NSString`. – rmaddy Jul 23 '14 at 17:38
  • @rmaddy Okay, so I'm here: http://i.stack.imgur.com/EobVV.png in UIInputViewController.m file. How does UILexicon work now? What goes in the block statement? And how do I spell check a mis-spelled word? – Albert Renshaw Jul 23 '14 at 17:44
  • First - use Xcode's code completion to get the syntax of the completion handler parameter correct. That will fix your syntax errors. Second - I have no idea how to use it. I'm not writing a custom keyboard. – rmaddy Jul 23 '14 at 17:45
  • @rmaddy Thanks, still can't figure it out though with code completion. – Albert Renshaw Jul 23 '14 at 18:43
  • On another note, why is everyone voting to close this question? Because nobody knows how to do it? Haha, I thought that was the point of this site – Albert Renshaw Jul 23 '14 at 18:43
  • What documentation are you finding unhelpful, and how is it failing to help you? Apple can't make the docs address your needs better if you don't tell them what you need. You can report issues with the docs [with radar](http://bugreport.apple.com) just like you can for software. And every documentation page has a "Feedback" link in the lower right corner. – rickster Jul 23 '14 at 18:58
  • @rickster I don't understand how to implement this. There isn't really any examples in the documentation or online anywhere. https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIInputViewController_Class/index.html#//apple_ref/occ/instm/UIInputViewController/requestSupplementaryLexiconWithCompletion: – Albert Renshaw Jul 23 '14 at 19:10
  • @rickster the `requestSupplementaryLexiconWithCompletion:` section in that of course :) – Albert Renshaw Jul 23 '14 at 19:10

1 Answers1

14

I've never used this feature, but a quick web search for "UILexicon" landed me in Apple's documentation; reading and following links from there filled in the picture pretty quick.

App Extension Programming Guide has a quick explanation of what lexicons are for:

Every custom keyboard (independent of the value of its RequestsOpenAccess key) has access to a basic autocorrection lexicon through the UILexicon class. Make use of this class, along with a lexicon of your own design, to provide suggestions and autocorrections as users are entering text.

Clicking the UILexicon link on that page took me to the reference doc for that class, which explains that it's a read-only list of Apple-provided term pairs. Each of its entries is a UILexiconEntry object -- the docs for that class say it provides a userInput (what the user typed, e.g. "ipad") and a documentText (what to substitute for it, e.g. "iPad"). Since those classes are read-only, it follows that they're probably not a way for you to provide your own autocorrection pairs -- as stated in the docs, they're for supplementing whatever autocorrection system you implement.


At this point, I don't even have to look at the doc for requestSupplementaryLexiconWithCompletion: to get a good idea how to use it: just the declaration tells me:

  • It's a method on UIInputViewController, the class I'd have to subclass to create a custom keyboard. Somewhere in that subclass I should probably call it on self.
  • Its return type is void, so I can't get a lexicon by assigning the result of a requestSupplementaryLexiconWithCompletion call to to a variable.
  • It calls the block I provide, passing me a UILexicon object as a parameter to that block.
  • It's got words like "request" and "completionHander" in it, so it'll probably do something asynchronous that takes awhile, and call that block when it's done.

So, I'm guessing that if I were writing a custom keyboard, I'd call this method early on (in viewDidLoad, perhaps) and stash the UILexicon it provides so I can refer to it later when the user is typing. Something like this:

@property UILexicon *lexicon;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestSupplementaryLexiconWithCompletion:^(UILexicon *lexicon){
        self.lexicon = lexicon;
    }];
}

Because it's unclear how long requestSupplementaryLexiconWithCompletion will take to complete, any place where I'm using self.lexicon I should check to see if it's nil.


Back in the App Extension Programming Guide, it lists "Autocorrection and suggestion" under "Keyboard Features That iOS Users Expect", right before saying:

You can decide whether or not to implement such features; there is no dedicated API for any of the features just listed

So it sounds like autocorrection is something you have to do yourself, with your own UI that's part of the view presented by your UIInputViewController subclass. The API Quick Start for Custom Keyboards section in the programming guide seems to hint at how you'd do that: use documentContextBeforeInput to see what the user has recently typed, deleteBackward to get rid of it, and insertText: to insert a correction.

rickster
  • 124,678
  • 26
  • 272
  • 326
  • Hi, first off thank-you so much for the time you took to write this detailed answer. This is actually how I was planning on using the UILexicon assuming it was a NSDictionary of sorts. :) I still am not completely sure I understand how to get the UILexicon object though. In UIInputViewController.m in `viewDidLoad` I call what? `UILexicon *theLexicon = (UILexicon *)[self requestSupplementaryLexiconWithCompletion:nil];` ? – Albert Renshaw Jul 24 '14 at 00:58
  • you mentioned block statements so I know this isn't the case, I just don't know what to put where I put `nil` ... something along the lines of `^() { }` right? I think that part is the completion handler but I still don't know how to format it without getting errors in xCode. I also have no clue where to include the request section unfortunately. This is the part I'm struggling with :) – Albert Renshaw Jul 24 '14 at 01:00
  • Ah, so you're unfamiliar with blocks. [Apple has a doc for that.](https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html) And I'll edit my answer... – rickster Jul 24 '14 at 18:20
  • Thanks! So now I have it working an I have a UILexicon object... I gather the entries and view the strings, but when I write a little while loop to return them all in the log just to make sure I have a whole dictionary worth of words (haha) it only returns like 12 words... so how am I supposed to use this for auto-correct like Apple docs suggest I should? There is only like 15 words, so 99% of mis-spelled words won't even be on the list. --> code: http://i.stack.imgur.com/jzjS4.png – Albert Renshaw Jul 25 '14 at 22:12
  • Did you see the part in the docs (and even the name of the method) about it being *supplementary*? "Regular" autocorrection is four a custom keyboard to implement itself — it's your opportunity to differentiate your keyboard from others. The lexicon is there to provide device-, user-, and situation-specific corrections in addition to your own. – rickster Jul 26 '14 at 05:14
  • A genuinely excellent answer. I just stumbled upon UILexicon today and you covered the key points very nicely. The "standard" keyboard features seem to have been left out of iOS 8's custom keyboard API. I think this is a great oversight. I venture that I'll open source a library to remedy this. – Daniel Brim Sep 11 '14 at 03:25