INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Predictive Text Problem

Predictive Text Problem

(OP)
Hi, I have a small problem with a predictive text email field I'm trying to write.
It is sick (in a bad way), and here are the symptoms.

If my address book has the names David Smith, and Richard Smidgens, if I type in the characters "Sm", it behaves properly, seemingly, and shows both names, as options.

The problem, if you'll look closer, is that it's actually populated the field with the first two letters of the first name it's found.

If I type in a third letter "i", instead of it keeping both name (Smith/Smidgens), it actually adds the i, to the first two letters of the first name, giving me "Dai", and removing the two names from contention for predictability.

Any idea what I'm doing wrong here? Here's the code for this section

Any help would be greatly appreciated... thank you.

-Phil

---
/**
    Creates the array of items used in the combo box of search results.
*/
- (NSArray*) createMatchesText: (NSArray*) searchResults
{    
    NSMutableArray* matchesArray = [NSMutableArray array];
    
    size_t i;
    for (i = 0; i < [searchResults count]; ++i)
    {
        id obj = [searchResults objectAtIndex: i];
        
        NSString* first = 0;
        NSString* last = 0;
        NSString* email = 0;
        
        @try
        {
            first = [obj valueForProperty: @"First"];
        }
        @catch (NSException* e)
        {
        }
        
        @try
        {
            last = [obj valueForProperty: @"Last"];
        }
        @catch (NSException* e)
        {
        }
        
        @try
        {
            id emails = [obj valueForProperty: @"Email"];
            if (emails)
            {
                NSString* primaryId = [emails primaryIdentifier];
                int index = [emails indexForIdentifier:primaryId];
                
                email = [(ABMultiValue*)emails valueAtIndex: index];
            }
        }
        @catch (NSException* e)
        {
        }
        
        
        NSMutableString* str = [[NSMutableString alloc] init];
        
        if (first)
        {
            [str appendString: first];
        }
        
        if (last)
        {
            [str appendString: [NSString stringWithFormat: @" %@", last]];
        }
        
        if (email)
        {
            [str appendString: [NSString stringWithFormat: @" <%@>", email]];
        }
        
        [matchesArray addObject: str];
    }
    
    return matchesArray;
}


- (NSArray *)tokenFieldCell: (NSTokenFieldCell*)tokenFieldCell
    completionsForSubstring: (NSString*)substring
               indexOfToken: (long)tokenIndex
        indexOfSelectedItem: (long*)selectedIndex
{
    // See if anything matches in the address book
    ABAddressBook* ab = [ABAddressBook sharedAddressBook];
    
    NSMutableArray* elms = [[NSMutableArray alloc] init];
    
    ABSearchElement* firstName = [ABPerson searchElementForProperty:kABFirstNameProperty
                                                              label:nil
                                                                key:nil
                                                              value: substring
                                                         comparison: kABPrefixMatchCaseInsensitive];
    
    ABSearchElement* lastName = [ABPerson searchElementForProperty:kABLastNameProperty
                                                             label:nil
                                                               key:nil
                                                             value: substring
                                                        comparison: kABPrefixMatchCaseInsensitive];

    ABSearchElement* email = [ABPerson searchElementForProperty:kABEmailProperty
                                                             label:nil
                                                               key:nil
                                                             value: substring
                                                        comparison: kABPrefixMatchCaseInsensitive];
    
    [elms addObject: firstName];
    [elms addObject: lastName];
    [elms addObject: email];
    
    ABSearchElement* searchElm = [ABSearchElement searchElementForConjunction:kABSearchOr children:elms];
    
    NSArray* searchResults = [[ab recordsMatchingSearchElement:searchElm] retain];
    
    NSArray* matches = [self createMatchesText: searchResults];
    
    if ([matches count] > 0) {
        *selectedIndex = 0;
    }
    else {
        *selectedIndex = -1;
    }
    
    return matches;
}


- (NSTokenStyle)tokenFieldCell: (NSTokenFieldCell *)tokenFieldCell styleForRepresentedObject: (id)representedObject
{
    return NSPlainTextTokenStyle;
}


 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close