1
홈 버튼을 눌렀을 때 데이터를 저장하려고합니다. 여기 관련 코드가 있습니다.파일에서 데이터를 읽는 중 'NSRangeException'이 발생했습니다.
- (NSString *) saveFilePath
{
NSArray *path =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[path objectAtIndex:0] stringByAppendingPathComponent:@"savefile.plist"];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSArray *values = [[NSArray alloc] initWithObjects:
[NSNumber numberWithInt:askedQuestions], //for questionLabel
[NSNumber numberWithInt:timeMin], //timeMin
[NSNumber numberWithInt:timeSec], //timeSec
[NSNumber numberWithInt:skipCount], //skipped question
[NSNumber numberWithInt:correctAnswers], //corectAnswers
nil];
[values writeToFile:[self saveFilePath] atomically:YES];
}
saveFilePath 메서드가 파일 경로를 제공하고 있습니다.
그리고있는 viewDidLoad
NSString *myPath = [self saveFilePath];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:myPath];
if (fileExists)
{
NSArray *values = [[NSArray alloc] initWithContentsOfFile:myPath];
_readerLabel.text = [[values objectAtIndex:0] stringValue];
askedQuestions=[[values objectAtIndex:0] intValue];
timeMin=[[values objectAtIndex:1]intValue];
timeSec=[[values objectAtIndex:2]intValue];
skipCount=[[values objectAtIndex:3]intValue];
correctAnswers=[[values objectAtIndex:4]intValue];
}
UIApplication *myApp = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:myApp];
그리고 난이 무엇입니까에서
: 코드에서 문제가Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray objectAtIndex:]: index (0) beyond bounds (0)'
*** First throw call stack:
(0x16a1012 0x13aee7e 0x16a0deb 0x16957e0 0x6891 0x312d 0x305817 0x305882 0x254a25 0x254dbf 0x254f55 0x25df67 0x221fcc 0x222fab 0x234315 0x23524b 0x226cf8 0x2703df9 0x2703ad0 0x1616bf5 0x1616962 0x1647bb6 0x1646f44 0x1646e1b 0x2227da 0x22465c 0x26cd 0x25f5)
libc++abi.dylib: terminate called throwing an exception
. 도와주세요!
감사를 요구하고
. objectAtIndex = 0 만 사용하면 코드가 올바르게 작동하고 인덱스 1부터 충돌이 발생합니다. –
그럼 파일에서 반환 된 배열에는 하나의 객체 만 포함되어 있다고 생각합니다 :) – deanWombourne
만든 파일을 삭제했는데 모든 것이 지금입니다. 도움을 주신 @ 감사합니다. –