我在iPhone上写大文件时遇到问题。我的代码在模拟器上工作得很好,但在iPhone上没有任何反应,文件大小为0! 我认为这取决于文档目录,但我不确定。
for (int i = 1; i <= [_anzahl.text intValue]; i++)
{
NSString *book = [@"book" stringByAppendingFormat:@"%i", i];
NSLog(@"%@", book);
NSString *dummyFilePath = [[NSBundle mainBundle] resourcePath];
NSString *filePath = [dummyFilePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", book]];
NSFileHandle *file = [NSFileHandle fileHandleForWritingAtPath:filePath];
if (file == nil) {
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
file = [NSFileHandle fileHandleForWritingAtPath:filePath];
}
int zahl = [_sizeMB.text intValue];
[file seekToFileOffset:zahl * 1024 * 1024];
const unsigned char bytes[] = {1};
[file writeData:[NSData dataWithBytes:bytes length:sizeof(bytes)]];
//Dateigröße ermitteln
int fileSize = [file seekToEndOfFile];
NSLog(@"book Size =%i" , fileSize) ;
}
所有新手的更新:
NSString *dummyFilePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];