2012-10-31 85 views
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

你知道的當前目錄運行應用程序是?如果這些文件在您的包中,那麼您需要獲取包路徑而不是當前目錄。 – trojanfoe

+0

對,你幾乎不用iPhone上的currentDirectoryPath。它主要是因爲歇斯底里的原因。 –

+0

即使NSString * pathToFile = [NSString stringWithString:@「sells」]; (文件與應用程序在同一目錄中)它返回否:( –

回答

0

這是你通常如何訪問一個文件中的應用程序包:

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

而且這是你應該如何訪問一個目錄:

NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
    NSString *path = [documentsDir stringByAppendingPathComponent:@"fileName"]; 
    if(![fileManager fileExistsAtPath:path]) 
    { 
     // foo bar 
    }