以下是示例代码。我如何使用这个变量:“Readme.txt”作为文件名? 在此先感谢。如何在xcode中使用NSString中的文件名变量?
NSString *file_name = [NSString stringWithFormat:@"Readme"];
NSString *filePath=[[NSBundle mainBundle] pathForResource:@"%@",file_name ofType:@"txt"];
以下是示例代码。我如何使用这个变量:“Readme.txt”作为文件名? 在此先感谢。如何在xcode中使用NSString中的文件名变量?
NSString *file_name = [NSString stringWithFormat:@"Readme"];
NSString *filePath=[[NSBundle mainBundle] pathForResource:@"%@",file_name ofType:@"txt"];
你在做什么错的是,
NSString *filePath=[[NSBundle mainBundle] pathForResource:@"%@",file_name ofType:@"txt"];
的而不是代码,
NSString *filePath=[[NSBundle mainBundle] pathForResource:file_name ofType:@"txt"];
你可以简单地传递变量。
NSString *filePath = [[NSBundle mainBundle] pathForResource:file_name ofType:@"txt"];
而且,由于要定义file_name
是一个常量字符串,你不需要使用stringWithFormat:
方法。你可以直接定义它。
NSString *file_name = @"Readme";
如果file_name
变量将是自述每一次,那么它的恒定,并不需要在所有定义:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Readme" ofType:@"txt"];
NSString *file_name = @"Readme.txt";
NSString *name = [file_name stringByDeletingPathExtension];
NSString *extension = [file_name pathExtension];
NSString *file_path = [[NSBundle mainBundle] pathForResource:name ofType:extension];
NSString *filePath=[[NSBundle mainBundle] pathForResource:@"Readme" ofType:@"txt"];
谢谢。它的工作:) – sgoldman 2011-05-06 03:13:03
@sgoldman:你欢迎:) – KingofBliss 2011-05-06 03:14:22