Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7948453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:43:34+00:00 2026-06-04T01:43:34+00:00

I have created a custom UITextField that compares user input against database records. Everything

  • 0

I have created a custom UITextField that compares user input against database records. Everything works great until the user puts a space in the text field after I have already suggested something. It crashes the app with the following message:

Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘* -[__NSCFConstantString substringToIndex:]: Range or index out of bounds’

Here is the code:

NSRange cursorRange = [[self valueForKey:@"selectionRange"] rangeValue];
NSString *entered = [self.text substringToIndex:cursorRange.location];
if (![self.text isEqualToString:entered]) {
    self.text = entered;
    return;
}

NSString *suggestedSuffix;
for (NSString *name in filterData) {
    if (name.length > cursorRange.location && [[name lowercaseString] hasPrefix:entered]) {
        suggestedSuffix = [name substringFromIndex:cursorRange.location];
        NSMutableString *suggestedString = [[NSMutableString alloc] initWithString:entered];
        [suggestedString appendString:suggestedSuffix];
        self.text = suggestedString;
        [self setValue:[NSValue valueWithRange:cursorRange] forKey:@"selectionRange"];
        return;
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:43:36+00:00Added an answer on June 4, 2026 at 1:43 am

    I’m wondering whether you have your key correct?

    UITextField does not have a selectionRange property publicly defined AFAICT. However, <UITextInput> – to which UITextField conforms – has a selectedTextRange property. Is this perhaps what you intended? (It’s possible there’s an undocumented private property actually called selectionRange that you’ve accidentally encountered, and which doesn’t behave as expected in cases such as you encountered.)

    Alternatively, did you really mean UITextView – which has a selectedRange – but still no selectionRange?

    On a separate note, I don’t think subclassing UITextField is the right approach here. Autocompletion can be completely (no pun intended) implemented using the <UITextFieldDelegate> methods and it is a maxim of Cocoa programming that when you can accomplish a goal through class composition rather than subclassing, you should opt for composition every time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom ExpandableListAdapter and everything works properly. What I'd like to
I have created a custom module that creates a custom Database table. I have
I have created custom drawable marker which uses canvas to draw bounds. Everything works
I have created a custom workflow activity that copies attachments from a case to
I have created a custom tag that looks like this: def textField = {
I have created a custom attribute that I am using on my class MyClass
I have created custom listview in that have list of textview & list of
I have created Custom Dialog for my application. While i run that application in
I have created Custom User Control which contain TextBox and PasswordBox. it is binding
I have created a custom list in SharePoint 2010 programmatically. It works fine on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.