NSRangeException即使在NSString Replace中范围是正确的

范围

我写了一个代码来替换删除NSString中的子字符串...我的逻辑是使用'rangeofString'查找每个子字符串字符的位置,然后使用'ReplaceOccurance'用空格替换子字符串字符,然后修剪空白字符。但是在replaceOccurance行中出现错误

*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString replaceOccurrencesOfString:withString:options:range:]: Range or index out of bounds'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff85614b06 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff83c4e3f0 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff856148dc +[NSException raise:format:] + 204
    3   CoreFoundation                      0x00007fff85613520 -[__NSCFString replaceOccurrencesOfString:withString:options:range:] + 224
    4   Foundation                          0x00007fff8d433a12 -[NSString stringByReplacingOccurrencesOfString:withString:options:range:] + 170
    5   tester                              0x00000001000019a1 deleteWithSubstring + 865
    6   tester                              0x0000000100001d35 main + 133
    7   libdyld.dylib                       0x00007fff89e267e1 start + 0
    8   ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminate called throwing an exception....

这是我的代码。

NSString* deleteWithSubstring(NSString *s1,NSString *sub)
{
    long int length=[sub length],range=0;
    NSRange r;
    for(int i=0;i<length;i++)
    {
        range=[s1 rangeOfString:[NSString stringWithFormat:@"%c",[sub characterAtIndex:i]]options:NSCaseInsensitiveSearch range:NSMakeRange(range,[s1 length]-1)].location;


        r=NSMakeRange(range,[s1 length]-1);
        NSLog(@"%ld %ld",r.location,r.length);
       if(range!=NSNotFound)
       {
           s1=[s1 stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%c",[sub characterAtIndex:i]] withString:@" " options:NSCaseInsensitiveSearch range:r];
       }
    }
    s1=[s1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    return s1;
}

int main(int argc, const char * argv[])
{

    @autoreleasepool {


        NSString *str1,*str2;
        str1=@"Hdida";
        str2=@"fHsdai";

        NSLog(@"Loc B:%@ ",,deleteWithSubstring(str2,@"ha"));


    }
    return 0;
}

实际上我解决了范围问题。由于重写代码,我误认为seocond参数不是上限,而是长度..应如下所示

   NSString* deleteWithSubstring(NSString *s1,NSString *sub)
    {
        long int length=[sub length],range=0,ndeleted=0;
        NSMutableString *s2=[[NSMutableString alloc]initWithString:s1];
        NSRange r;
        for(int i=0;i<length;i++)
        {
            range=[s1 rangeOfString:[NSString stringWithFormat:@"%c",[sub characterAtIndex:i]]options:NSCaseInsensitiveSearch range:NSMakeRange(range,[s1 length]-range)].location;


            r=NSMakeRange((range-ndeleted),1);
            NSLog(@"%ld %ld" ,r.location,r.length);
           if(range!=NSNotFound)
           {
               [s2 deleteCharactersInRange:r];
               ndeleted++;
           }
        }

        NSLog(@"%@",s2);
        return s2;
    }
舒沃

试试下面的代码,

NSString* deleteWithSubstring(NSMutableString *s1,NSString *sub)
{

    NSRange r;
    for(int i = 0; i < [s1 length]; i++)
    {
        //findout the range with "sub" string
        r = [s1 rangeOfString:sub options:NSCaseInsensitiveSearch];

        if(r.location != NSNotFound)
        {
            //Delete the characters in that range
            [s1 deleteCharactersInRange:r];
        }
        else
        {
            //break the loop if sub string not found as there is no more recurrence.
            break;
        }
    }
    return s1;
}

int main(int argc, char *argv[])
{

    @autoreleasepool {
        //initiating str1 here
        NSMutableString* str1 = [[NSMutableString alloc] init];
        [str1 appendString:@"fHsdaihati"];

        NSLog(@"Loc B:%@ ",deleteWithSubstring(str1,@"ha"));
    }
    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

即使我有正确的范围,cant_delete_file?

swift 中 NSRangeException 的错误句柄

NSRangeException索引9超出范围[0 .. 8]'

如何正确显示到NSTextView中?NSString / NSInputStream / NSTextView

NSString的范围,更改html标签

即使在理解范围之后,列表理解也会重新绑定名称。这是正确的吗?

即使值超出其范围,整数变量也如何显示正确的值?

类未在范围中声明-即使包含.h

即使不在范围内,也不断观察eclipse中的变量

即使行显示0,在SQL中显示时间范围的计数

IndexError:在python中,即使范围存在,列表索引也超出范围

即使正确的参数,调用中的参数数目或类型也错误

即使逻辑比较正确,R中的加入也会失败

“双引号”无法正确显示:即使在全新安装中

-[NSManagedObjectContext保存:]中的数组索引越界问题(NSRangeException)

多线程块中的核心数据NSRangeException崩溃

(NSString *)它是在Pascal中是指针还是NSString?

检查NSString中是否包含NSString hasPrefix

Excel VBA中的范围不正确

在NSString中写入%

从 NSString 中获取值

IF Else方法,在NSString中

未在此范围内声明“ NSString”

'NSRangeException',原因:'***-[__ NSArrayM objectAtIndex:]:索引1超出范围[0 .. 0]'

异常'NSRangeException',原因:'***-[__ NSArrayM objectAtIndex:]:索引19超出范围[0 .. 18]'

非空数组的NSRangeException索引1超出范围[0 .. 0]

NSRangeException Xcode

即使语法正确也反应错误

即使正确也显示其他