【问题标题】:reading Local File on iPhone simulator crashes, but not on real device在 iPhone 模拟器上读取本地文件崩溃,但不是在真实设备上
【发布时间】:2012-07-01 19:54:01
【问题描述】:

我正在开发一个 iphone 应用程序,它从服务器下载 UTF8Encoded 文件,将其存储在应用程序的缓存文件夹中,然后简单地将其内容复制到 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]);

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

File loaded into path: /Users/username/Library/Application Support/iPhone Simulator/5.1/Applications/4FCF8FC6-4F1B-4FE5-92F6-A99EC8888E47/Library/Caches/utf8encodedFile.txt

但是在调用“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

【问题讨论】:

  • 你确定文件存在吗?
  • 当然,我已经检查了它,手动转到终端中的缓存目录,并使用 NsFileManager 方法以编程方式检查它是否存在。清除缓存并再次上传文件没有帮助。在设备上它可以完美运行,但我不喜欢用我的个人设备测试我的应用程序:(

标签: iphone file ios-simulator


【解决方案1】:

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

NSData dataWithContentsOfFile:

和NSString

- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding;

【讨论】:

    猜你喜欢
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 2015-07-11
    相关资源
    最近更新 更多