46

I am setting the attributed text of a label, and I am getting this strange error: Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray replaceObjectsInRange:withObject:length:: Out of bounds'. I have never seen this error before, so I am not sure how to fix it. Here is the code causing the crash:

if ([cell.waveTextLabel respondsToSelector:@selector(setAttributedText:)])
{
    const CGFloat fontSize = 16.0f;

    //define attributes
    UIFont *boldFont = [UIFont fontWithName:@"HelveticaNeue-Medium" size:fontSize];

    // Create the attributes
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:boldFont, NSFontAttributeName, nil];

    NSRange rangeOfIs = [cell.cellWaveObject.waveString rangeOfString:@" is"];
    NSLog(@"The range of is: {%lu, %lu}", (unsigned long)rangeOfIs.location, (unsigned long)rangeOfIs.length);
    NSRange nameRange = NSMakeRange(0, rangeOfIs.location);
    NSLog(@"The range of the name: {%lu, %lu}", (unsigned long)nameRange.location, (unsigned long)nameRange.length);

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:cell.cellWaveObject.waveString];
    [attributedString setAttributes:attrs range:nameRange];

    //set the label text
    [cell.waveTextLabel setAttributedText:attributedString]; 
}

I set a exception breakpoint, and it is crashing on the line [attributedString setAttributes:attrs range:nameRange]; does anyone know how I can fix this?

3

2 Answers 2

0

The error definetly means the range is out of bounds of the string, could you try to store in a local variable the string you're trying to apply. Also you could try substringWithRange to see if you're using a correct range. If the range is correct it shouldn't show that error.

0

The problem "Terminating app due to uncaught exception 'NSRangeException'" often happens when you attempt to read or modify entries in an array or string using an out-of-bounds index.

In your code, the issue might be with the calculation of the range for setting attributes. Specifically, when you're calculating the nameRange, you're using the location property of rangeOfIs as the length of the range. This can lead to an out-of-bounds error if rangeOfIs does not contain the substring " is", very obvious.

To fix this, you should check if the substring " is" exists in cell.cellWaveObject.waveString before attempting to set the range. Additionally, you should also ensure that rangeOfIs.location is not equal to NSNotFound, indicating that the substring was found. Here's how you can modify your code to handle this:

if ([cell.waveTextLabel respondsToSelector:@selector(setAttributedText:)]) {
    const CGFloat fontSize = 16.0f;
    UIFont *boldFont = [UIFont fontWithName:@"HelveticaNeue-Medium" size:fontSize];
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:boldFont, NSFontAttributeName, nil];

    NSRange rangeOfIs = [cell.cellWaveObject.waveString rangeOfString:@" is"];
    
    // Check if " is" substring exists and its location is not NSNotFound
    if (rangeOfIs.location != NSNotFound) {
        NSRange nameRange = NSMakeRange(0, rangeOfIs.location);
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:cell.cellWaveObject.waveString];
        [attributedString setAttributes:attrs range:nameRange];
        [cell.waveTextLabel setAttributedText:attributedString];
    } else {
        // Handle case when " is" substring is not found
        NSLog(@"Substring ' is' not found in wave string");
        // You may want to set a default attributed text or handle it differently
    }
}

This modification ensures that the range you're setting for attributes is within the bounds of the string and handles the case when the substring " is" is not found in cell.cellWaveObject.waveString.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.