Objective c 编译后的程序可以';找不到文件
它在XCode中正常工作,但my.app总是返回NO(“sells”当然存在于它的目录中)。如何解决此问题?这是您通常访问应用程序包中单个文件的方式:Objective c 编译后的程序可以';找不到文件,objective-c,osx-snow-leopard,Objective C,Osx Snow Leopard,它在XCode中正常工作,但my.app总是返回NO(“sells”当然存在于它的目录中)。如何解决此问题?这是您通常访问应用程序包中单个文件的方式: NSFileManager *fm=[NSFileManager defaultManager]; NSString *pathToFile=[NSString stringWithFormat:@"%@/sells", [fm currentDirectoryPath]]; if ([fm fileExistsAtPath:pathToF
NSFileManager *fm=[NSFileManager defaultManager];
NSString *pathToFile=[NSString stringWithFormat:@"%@/sells", [fm currentDirectoryPath]];
if ([fm fileExistsAtPath:pathToFile] == NO)
{
return NO;
}
else
{
if(content)
{
[content release];
}
content=[[NSMutableString alloc] initWithContentsOfFile:pathToFile encoding:NSUTF8StringEncoding error:nil];
}
return YES;
下面是访问目录的方式:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"yourFile" ofType:@"ext"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
你知道正在运行的应用程序的当前目录是什么吗?如果这些文件在您的捆绑包中,那么您需要获取捆绑包路径,而不是当前目录。这主要是因为歇斯底里的原因。即使NSString*pathToFile=[nsstringstringwithstring:@“sells”];(文件与应用程序位于同一目录中)它返回否:(
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [documentsDir stringByAppendingPathComponent:@"fileName"];
if(![fileManager fileExistsAtPath:path])
{
// foo bar
}