0
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;

它在 XCode 中正常工作,但 my.app 总是返回 NO(“sells”当然存在于它的目录中)。如何解决?

4

1 回答 1

0

This is how you normally access a single file in the App bundle:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"yourFile" ofType:@"ext"];  
NSData *fileData = [NSData dataWithContentsOfFile:filePath];  

And this is how you should access a directory:

    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *path = [documentsDir stringByAppendingPathComponent:@"fileName"];
    if(![fileManager fileExistsAtPath:path])
    {
        // foo bar
    }
于 2012-10-31T11:40:38.050 回答