在iPhone模拟器上阅读本地文件崩溃,但不在真实设备上崩溃

use*_*309 5 iphone file ios-simulator

我正在开发一个UTF8Encoded从服务器下载文件的iPhone应用程序,将其存储在App的Cache文件夹中,只需将其内容复制到NSString* var.它适用于设备,但不适用于模拟器,这里代码:

NSLog(@"File loaded into path: %@\n", localPath);
NSError* error;
NSString* tmpString = [NSString stringWithContentsOfFile:localPath encoding:NSUTF8StringEncoding error:&error];
NSLog(@"Error: %@", error);
//Prints the length in the console to check if the file has been correctly copied in the string
NSLog(@"tmpString length:%u", [tmpString length]);
Run Code Online (Sandbox Code Playgroud)

在模拟器上,这部分代码打印出来:

File loaded into path: /Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/4FCF8FC6-4F1B-4FE5-92F6-A99EC8888E47/Library/Caches/utf8encodedFile.txt
Run Code Online (Sandbox Code Playgroud)

但它在调用" stringWithContentsOfFile:"方法时崩溃,没有显示任何错误

在真实设备上一切正常,它打印:

File loaded into path: /var/mobile/Applications/1EE8AEEB-D036-4ADE-AE12-836BA1F16BCB/Library/Caches/utf8encodedFile.txt
2012-06-30 19:19:24.743 appName[685:707] 
Error: (null)
2012-06-30 19:19:24.745 appName[685:707] tmpString length:1423
Run Code Online (Sandbox Code Playgroud)

小智 0

您是否尝试过将文件的内容加载到 anNSData然后NSString用它创建 an ?

NSData dataWithContentsOfFile:
Run Code Online (Sandbox Code Playgroud)

NSString

- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding;
Run Code Online (Sandbox Code Playgroud)