NSString substringWithRange 崩溃

发布于 2024-11-26 14:09:35 字数 1444 浏览 1 评论 0原文

我有这样的代码:

- (void)splitAndSendString:(NSString *)string
             withAlignment:(UITextAlignment)alignment
         verticalExpansion:(NSUInteger)verticalExpansion
       horizontalExpansion:(NSUInteger)horizontalExpansion
                     inRed:(BOOL)isRed
          leftBumperString:(NSString *)leftString
         rightBumperString:(NSString *)rightString
                lineLength:(NSUInteger)lineLength
{
    NSInteger charactersLeftAfterString = lineLength - [string length] -
            [leftString length] - [rightString length];

   // if either of the bumpers is nil, then replace it with @""
   //so that we don't get "null" printed
   if (leftString == nil)  { leftString = @"";     }
   if (rightString == nil) { rightString = @"";    }

   if (charactersLeftAfterString < 0) {
        NSInteger charactersAvailableForString =
           [string length] + charactersLeftAfterString;
        [self sendString:[NSString stringWithFormat:@"%@%@%@", leftString, [string substringWithRange:NSMakeRange(0, charactersAvailableForString)], rightString] withAlignment:UITextAlignmentLeft verticalExpansion:verticalExpansion horizontalExpansion:horizontalExpansion inRed:isRed];
   }
   //(a lot of other code here that isn't relevant)
}

由于 NSRange 错误,它在 sendString 方法上崩溃。我不明白这是怎么可能的,因为在 if 语句的条件下,charactersLeftAfterString 必然小于 0,所以charactersAvailableForString 总是小于字符串长度,那么范围怎么可能比字符串长度长呢?

I have this code:

- (void)splitAndSendString:(NSString *)string
             withAlignment:(UITextAlignment)alignment
         verticalExpansion:(NSUInteger)verticalExpansion
       horizontalExpansion:(NSUInteger)horizontalExpansion
                     inRed:(BOOL)isRed
          leftBumperString:(NSString *)leftString
         rightBumperString:(NSString *)rightString
                lineLength:(NSUInteger)lineLength
{
    NSInteger charactersLeftAfterString = lineLength - [string length] -
            [leftString length] - [rightString length];

   // if either of the bumpers is nil, then replace it with @""
   //so that we don't get "null" printed
   if (leftString == nil)  { leftString = @"";     }
   if (rightString == nil) { rightString = @"";    }

   if (charactersLeftAfterString < 0) {
        NSInteger charactersAvailableForString =
           [string length] + charactersLeftAfterString;
        [self sendString:[NSString stringWithFormat:@"%@%@%@", leftString, [string substringWithRange:NSMakeRange(0, charactersAvailableForString)], rightString] withAlignment:UITextAlignmentLeft verticalExpansion:verticalExpansion horizontalExpansion:horizontalExpansion inRed:isRed];
   }
   //(a lot of other code here that isn't relevant)
}

It's crashing on the sendString method, because of a bad NSRange. I don't understand how this is possible given charactersLeftAfterString is necessarily less than 0 given the condition of the if statement, so charactersAvailableForString is always less than the string length, so how can the range be longer than the string length?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仲春光 2024-12-03 14:09:35

您是否使用过调试器并检查了 charactersAvailableForString 的值?

...还是打印出来的?

...或者查看崩溃时的值?

Have you used the debugger and checked the value of charactersAvailableForString?

... or printed it?

... or looked at the value when it crashes?

南街女流氓 2024-12-03 14:09:35

如果你的字符串是空的,即 [string length] = 0

[string substringWithRange:NSMakeRange(0,charactersAvailableForString)]

每次都会给出 NSRange 异常,无论怎样,

charactersAvailableForString < 0

如果[字符串长度] = 5 且charactersLeftAfterString = -12,也会出现类似的问题。
字符串可用字符 = 5-12 = -7
[string substringWithRange:NSMakeRange(0,charactersAvailableForString)] 将给出 NsRange 异常。

If your string is empty, i.e [string length] = 0

[string substringWithRange:NSMakeRange(0, charactersAvailableForString)]

will always give a NSRange exception every time, no matter what as

charactersAvailableForString < 0

Also similar problems can arise if [string length] = 5 and charactersLeftAfterString = -12.
charactersAvailableForString = 5-12 = -7
[string substringWithRange:NSMakeRange(0, charactersAvailableForString)] will give NsRange Exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文