さて、これは少し混乱します(私にとっては)。私はそれから欲しい単一の数字を持っている文字列を持っています。後でその番号を取得できるように、この番号を「/」で囲みました。
これが私がそれから数を得る方法です:
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 0xd532 0x17d2f39 0x17d2c10 0x17ebda5 0x17ebb12 0x181cb46 0x181bed4 0x181bdab 0x17d1923 0x17d17a8 0x18e71 0x200d 0x1f35)libc ++ abi.dylib:例外をスローして終了します
範囲が範囲内にあることを数えると、なぜこのエラーが発生するのかわかりません。
どんな助けでもいただければ幸いです。
ありがとう