嗨,这是我的功能,但每次当我尝试初始化并分配foo
时,它会告诉我原因吗?
-(NSString*)modifyTheCode:(NSString*) theCode{
if ([[theCode substringToIndex:1]isEqualToString:@"0"]) {
if ([theCode length] == 1 ) {
return @"0000000000";
}
NSString* foo = [[NSString alloc]initWithString:[theCode substringWithRange:NSMakeRange(2, [theCode length]-1)]];
return [self modifyTheCode:foo];
} else {
return theCode;
}
}
错误消息:
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.2 (8H7)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).
答案 0 :(得分:1)
错误讯息是什么? 如果你正在使用NSRange,你可能应该首先检查代码的长度。
答案 1 :(得分:1)
替换此行
NSString* foo = [[NSString alloc]initWithString:[theCode substringWithRange:NSMakeRange(2, [theCode length]-1)]];
这一行
NSString* foo = [[NSString alloc]initWithString:[theCode substringWithRange:NSMakeRange(1, [theCode length]-1)]];
并尝试..
答案 2 :(得分:1)
因为范围无效。 NSRange有两个成员,位置和长度。您给出的范围从字符串的第三个字符开始,并且字符串的长度减去1。因此,您的长度比字符串中剩余的字符数长一个字符。
假设theCode为@"0123"
。您创建的范围是{ .location = 2, .length = 3 }
,代表:
0123
^ start of range is here
^ start of range + 3 off the end of the string.
顺便说一下,你会很高兴知道有方便的方法,所以你不必乱七八糟。你可以这样做:
if ([theCode hasPrefix: @"0"])
{
NSString* foo = [theCode substringFromIndex: 1]; // assumes you just want to strip off the leading @"0"
return [self modifyTheCode:foo];
} else {
return theCode;
}
顺便说一下,您的原始代码泄露了foo
,因为您从未发布过它。