NSMutableArray の特定の場所に文字列を挿入しようとしているので、実行しました
[myMutableArray insertObject:newString atIndex:219];
ところで、実行[myMutableArray count]
すると 273 が返されるので、十分な要素があることがわかります。ただし、問題は、これを試すたびに NSRangeException が発生することです。奇妙な部分は、ログに記録すると_[myMutableArray class]
_NSArrayM が返されることですが、例外が表示されると、[__NSCFString insertString:atIndex:] を呼び出していると表示され、それがエラーの原因です。私は間違いなくinsertObjectを使用していますが、それは間違いなく可変配列です。私の問題が何であるか知っている人はいますか?また、ブレークポイントを使用すると、投稿したコードの最初のビットが強調表示されるため、そこに例外があります。
allIDs = [self allIDs];
[allIDs insertObject:[newStudent idNumber] atIndex:x];
- (NSMutableArray*) allIDs
{
if (!allIDs) {
allIDs = [[NSMutableArray alloc] init];
}
filePath = [[NSString alloc] init];
NSError *error;
filePath = [[self getDocumentDirectory] stringByAppendingPathComponent:@"List of Student IDs"];
NSString *txtInFile = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUnicodeStringEncoding error:&error];
if(!txtInFile) {
return nil;
}
NSMutableString *tempName = [NSMutableString string];
for (int i = 0; i < [txtInFile length]; i++) {
if ([txtInFile characterAtIndex:i] == '*') {
[allIDs addObject:tempName];
tempName = [NSMutableString string];
} else {
[tempName appendFormat:@"%c", [txtInFile characterAtIndex:i]];
}
}
return allIDs;
}
そのため、戻って 219 までのすべてのインデックスに挿入するループを実行し、インデックスを出力して、どのインデックスで例外をスローし始めたかを確認しました。
for (int i = 0; i < 219; i++) {
NSLog(@"%i", i);
[allIDs insertObject:newStudent.idNumber atIndex:i];
}
何らかの理由で、218 までのすべてを問題なく出力し、801505682082758655 にスキップして、範囲外の例外が発生する前に何度もログに記録しました。