我正在开发一个从服务器下载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]);
在模拟器上,这部分代码打印出来:
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
答案 0 :(得分:0)
您是否尝试将文件内容加载到NSData
中,然后使用它创建NSString
?
NSData dataWithContentsOfFile:
和NSString
- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding;