두 줄의 숫자가 포함 된 텍스트 파일이 있습니다. 원하는 것은 각 줄을 문자열로 바꾼 다음 배열 (필드라고 함)에 추가하는 것입니다. 내 문제는 EOF 문자를 찾으려고 할 때 발생합니다. 문제없이 파일에서 읽을 수 있습니다 : 나는 그것의 콘텐츠를 NSString으로 변환 한 다음이 메서드로 전달합니다. 내 텍스트 파일의텍스트 파일에서 부분 문자열 만들기
-(void)parseString:(NSString *)inputString{
NSLog(@"[parseString] *inputString: %@", inputString);
//the end of the previous line, this is also the start of the next lien
int endOfPreviousLine = 0;
//count of how many characters we've gone through
int charCount = 0;
//while we havent gone through every character
while(charCount <= [inputString length]){
NSLog(@"[parseString] while loop count %i", charCount);
//if its an end of line character or end of file
if([inputString characterAtIndex:charCount] == '\n' || [inputString characterAtIndex:charCount] == '\0'){
//add a substring into the array
[fields addObject:[inputString substringWithRange:NSMakeRange(endOfPreviousLine, charCount)]];
NSLog(@"[parseString] string added into array: %@", [inputString substringWithRange:NSMakeRange(endOfPreviousLine, charCount)]);
//set the endOfPreviousLine to the current char count, this is where the next string will start from
endOfPreviousLine = charCount+1;
}
charCount++;
}
NSLog(@"[parseString] exited while. endOfPrevious: %i, charCount: %i", endOfPreviousLine, charCount);
}
내용은 다음과 같이 :
123
456
내가 첫 번째 문자열 (123) 아무 문제를 얻을 수 있습니다. 호출은 다음과 같습니다
[fields addObject:[inputString substringWithRange:NSMakeRange(0, 3)]];
다음으로, 두 번째 문자열에 대한 호출합니다
[fields addObject:[inputString substringWithRange:NSMakeRange(4, 7)]];
을하지만 오류가 발생, 나는 내 인덱스가 범위를 벗어 때문입니다 생각합니다. 인덱스가 0부터 시작하기 때문에 인덱스 7이 없습니다 (잘 EOF 문자라고 생각합니다). 오류가 발생합니다.
모든 것을 요약하면 : 6 자 + EOF 문자 만있는 경우 색인 색인 처리 방법을 모르겠습니다.
감사합니다. [ "\ n"@ inputString componentsSeparatedByString을]와 숫자의 배열을 얻을
-(NSArray*)parseString:(NSString *)inputString {
return [inputString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
}