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 8137621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:11:46+00:00 2026-06-06T11:11:46+00:00

I have an issue with NSRange that is causing my app to crash when

  • 0

I have an issue with NSRange that is causing my app to crash when textview is empty. I am using a custom keyboard that has a backspace button on it and it calls this code here…

if ([self.myChart isFirstResponder]) {
    NSRange currentRange = myChart.selectedRange;
    if (currentRange.length == 0) {
        currentRange.location--;
        currentRange.length++;
    }
    myChart.text = [myChart.text stringByReplacingCharactersInRange:currentRange withString:[NSString string]];
    currentRange.length = 0;
    myChart.selectedRange = currentRange;

    NSLog(@"%d", NSNotFound);
}

If I have text in the textview this works perfectly for removing one character at a time however when I get to the beginning of the textview and there are no more characters, I get the exception and crash…

*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString replaceCharactersInRange:withString:]: Range or index out of bounds'

My goal is to have the backspace function stop. I understand why it is crashing but I don’t know how to resolve it. I have tried evaluating it against NSNotFound without luck.

Any ideas would be much appreciated!

  • 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-06T11:11:48+00:00Added an answer on June 6, 2026 at 11:11 am

    You should check that there is text in the field, ie:

    if ( myChart.text.length > 0 )
    

    and also check to make sure you aren’t trying to access location -1:

    if ( currentRange.location >= 0 )
    

    So your code looks something like

    if ([self.myChart isFirstResponder] && myChart.text.length > 0 ) {
        NSRange currentRange = myChart.selectedRange;
        if (currentRange.length == 0) {
            currentRange.location--;
            currentRange.length++;
        }
        if ( currentRange.location >= 0 )
        {
            myChart.text = [myChart.text stringByReplacingCharactersInRange:currentRange withString:[NSString string]];
            currentRange.length = 0;
            myChart.selectedRange = currentRange;
    
            NSLog(@"%d", NSNotFound);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue that, while using sql server 2005 i have executed update
i have done drag and drop using with jquery. here i have issue that
I have an issue that is driving me a bit nuts: Using a UserProfileManager
I have issue with url rewrite. I have a site example.com that has a
I have issue that is reproduced on g++. VC++ doesn't meet any problems. So
I have an issue using jquerys selectors when it comes to a page I
I have issue wuth Firefox not displaying style text-decoration: line-through. I am using jqGrid
Have another issue here. Our app starts out with a tab activity. But, before
i have issue regarding Paging using filterexpression. here's the piece of code for filtering
MSSQL database. I have issue to create database using old databases data. Old database

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.