我想在一個txt寫的,但我的代碼沒有工作...的iOS寫一個txt
這是我的代碼:
-(void)bestScore{
if(cptScore > bestScore){
bestScore = cptScore;
highScore.text =[[NSString alloc] initWithFormat: @" %.d", bestScore];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"bestScore" ofType:@"txt"];
NSString *test = @"test";
[test writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
}
我已經叫bestScore.txt在一個文件中我的文件夾「支持文件」
你能幫助我嗎?
感謝
編輯: 我可以讀我的文件 「bestScore.txt」 與此代碼:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"bestScore" ofType:@"txt"];
NSString *textFromFile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
highScore.text = textFromFile;
bestScore = [textFromFile intValue];
什麼是「不工作」呢?它是否編譯?它會崩潰嗎?它是否有效,但不是寫作「測試」,而是寫下「所有的黑色史蒂夫喬布斯IPHONES的主人!」? – nhgrif
您無法在iOS中的應用程序包內寫入文件。你應該把你的文件寫入〜/ Library/Preferences或其他地方。 –
感謝您的答案。 這不工作,因爲沒有寫在我的txt,但我的應用程序運行沒有錯誤! – user3657063