在我的iPhone应用程序中,我使用文件管理器检查文件的大小,代码在模拟器上运行良好,但当我在iphone上运行相同的应用程序时,它显示文件大小为零,即使文件大小大于零,我使用的代码如下:
NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory1 = [paths1 objectAtIndex:0];
documentsDirectory1 = [documentsDirectory1 stringByAppendingPathComponent:@"XML"];
NSString * szDestPath1 = [documentsDirectory1 stringByAppendingPathComponent:@"Extras"];
NSString *URL2 = [szDestPath1 stringByAppendingPathComponent:@"config.xml"];
NSLog(@"%@",URL2);
NSError *attributesError = nil;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL2 error:&attributesError];
int _fileSize = [fileAttributes fileSize];
NSLog(@"_fileSize:%U",_fileSize); //0 if empty我该如何解决这个问题,有人能帮我吗?提前谢谢。
发布于 2011-03-24 20:47:39
您确定文件存在吗?
通常,如果涉及文件的一段代码在模拟器上工作正常,但在设备上却不能正常工作,那是因为文件名错误。大多数模拟器运行在不区分大小写的文件系统上,但设备具有区分大小写的文件系统。
因此,请再次检查您是否使用了正确的文件路径。
您可以添加类似以下内容的内容以确保该文件存在:
if (![[NSFileManager defaultManager] fileExistsAtPath:URL2]) {
NSLog(@"File does not exist");
}https://stackoverflow.com/questions/5418668
复制相似问题