好的,所以这有点令人困惑(对我来说很好)。我有一个字符串,其中包含一个我想要的数字。我用“/”包围了这个数字,以便以后可以从中取出这个数字。
这是我从中获取数字的方法:
if ([MYSTRING hasSuffix:@"mp"]) {
int start = 0;
int end = 0;
char looking = '/';
for(int i=0; i < MYSTRING.length; i++){
if (looking == [MYSTRING characterAtIndex:i]) {
if (start == 0) {
start = i;
}
else{
end = i + 1;
}
}
}
NSLog(@"%@", MYSTRING); //When i NSLOG here i get '2012-06-21 03:58:00 +0000/1/mp', 1 is the number i want out of the string, but this number could also change to 55 or whatever the user has
NSLog(@"start: %i, end: %i", start, end); //When i NSLOG here i get 'start: 25, end: 28'
NSString *number = [MYSTRING substringWithRange:NSMakeRange(start, end)];
number = [number stringByReplacingOccurrencesOfString:@"/" withString:@""];
if ([number intValue] > numberInt) {
numberInt = [number intValue];
}
它不断崩溃,控制台说:
*终止应用程序由于未捕获的异常'NSRangeException',原因是: ' - [__ NSCFString substringWithRange:]:范围或索引越界' *第一掷调用堆栈:(0x1875d72 0x106ce51 0x1875b4b 0x184ea64 0x3a6c 0x1080713 0x1bf59 0x1bef1 0xd532e 0xd588c 0xd49f5 0x49a2f 0x49c42 0x290fe 0x1b3fd 0x17d2f39 0x17d2c10 0x17ebda5 0x17ebb12 0x181cb46 0x181bed4 0x181bdab 0x17d1923 0x17d17a8 0x18e71 0x200d 0x1f35)libc++abi.dylib:终止调用抛出异常
从我的计数范围内,我不明白为什么我会收到这个错误?
任何帮助,将不胜感激。
谢谢