以下是示例代碼。我如何使用這個變量:「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