NSString substringWithRange 崩溃
我有这样的代码:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否使用过调试器并检查了
charactersAvailableForString
的值?...还是打印出来的?
...或者查看崩溃时的值?
Have you used the debugger and checked the value of
charactersAvailableForString
?... or printed it?
... or looked at the value when it crashes?
如果你的字符串是空的,即 [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.