How to resolve the NSRangeException Out of Bounds error
Asked Answered
F

2

47

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 an exception breakpoint, and it is crashing on the line [attributedString setAttributes:attrs range:nameRange]; does anyone know how I can fix this?

Frankie answered 23/8, 2013 at 19:55 Comment(3)
got the same error while my range is good...Interlock
Possible duplicate of NSMutableAttributedStrings - objectAtIndex:effectiveRange:: Out of boundsPapal
Is this Objective-C or Swift? Lang tags missing.Idealize
H
1

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.

Hardhack answered 8/3 at 9:36 Comment(0)
H
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.

Hake answered 20/2 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.