2016-01-21 86 views
-2

的main.m测试NSBundle为什么控制台输出(空)?

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"bookinf" 
                 ofType:@"txt"]; 
NSString* content = [NSString stringWithContentsOfFile:filePath 
                encoding:NSUTF8StringEncoding 
                error:nil]; 
    NSLog(@"file content : %@" , content); 

bookinf.txt内容: '<' C程序设计语言>

回答

0

您需要调试它在时间的一个步骤。 pathForResource如果无法找到该文件,则返回nil。如果filePath为零,则stringWithContentsOfFile将会失败(不知道它是否会返回零或崩溃)。

正如Anbu在他的回答中所说的,最可能的原因是您没有设置要包含在应用程序包中的文件。假设您将文件添加到项目中,请检查文件检查器并确保您的应用程序已在“目标成员身份”下进行检查