Search code examples
macoscocoaautocompletenstokenfield

NSTokenField suggestion is slow:what is the process?


NSTokenField takes quite a time to propose suggestion to the text I enter. More precisely, here is what happens:

  1. I type the letter "a"
  2. the method textDidBeginEditing: of NSTokenField is called immediately
  3. some time (approx. 1/2 to 1 second)
  4. the method tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem: is called and its execution is very quick.

I would like to understand what happens and possibly improve this situation.

My question:

What does happen between textDidBeginEditing: and tokenField:completionsForSubstring:indexOfToken:indexOfSelectedItem:?


EDIT

Very strange: if I type ";" instead of "a", it goes much quicker! even though I did the following:

- (NSArray *)        tokenField:(NSTokenField *)tokenField
        completionsForSubstring:(NSString *)substring
                   indexOfToken:(NSInteger)tokenIndex
            indexOfSelectedItem:(NSInteger *)selectedIndex
{
    return @[@"Hello"];
}

Solution

  • I think it is a bug of OSX 10.9.

    Here is what I did. The screen:

    enter image description here

    (the textField is a NSTokenField)

    Then, in a delegate for this NSTokenField, put the following code

    - (NSArray *)        tokenField:(NSTokenField *)tokenField
            completionsForSubstring:(NSString *)substring
                       indexOfToken:(NSInteger)tokenIndex
                indexOfSelectedItem:(NSInteger *)selectedIndex
    {
        return @[@"Jojo!!"];
    }
    
    
    
    - (id)                        tokenField:(NSTokenField *)tokenField
           representedObjectForEditingString:(NSString *)editingString
    {
        return @"Jojo the cat" ;
    }
    
    
    
    
    - (NSString *)              tokenField:(NSTokenField *)tokenField
         displayStringForRepresentedObject:(id)representedObject
    {
        return @"Jojo" ;
    }
    

    You'll see, it bugs.