iOS基礎:獲取資原始檔的方法
bundle是一個目錄,其中包含了程式會使用到的資源.這些資源包含了如影象,聲音,編譯好的程式碼,nib檔案(使用者也會把bundle稱為plug-in).對應bundle,cocoa提供了類NSBundle.
1.獲取影象聲音等檔案
//獲得本地檔案路徑
NSString * str = [[NSBundlemainBundle] pathForResource:@"爸比我要喝奶奶"ofType:@"mp3"];
//獲得本地檔案的路徑並轉url
NSURL *url = [[NSBundlemainBundle] URLForResource:@"ABC.mp3"withExtension:
2.獲取nib檔案
//獲取CustomCell.xib檔案內所有的檢視。
NSArray *nibs=[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
cell=[nibs objectAtIndex:0];//因為返回的是陣列
3.獲取Login.storyboard檔案內的根檢視(箭頭指向的控制器)。
id vc = [[UIStoryboardstoryboardWithName:@"Login"bundle:nil]instantiateInitialViewController
4.獲取主視窗的根檢視控制器
[UIApplicationsharedApplication].keyWindow.rootViewController = vc;
5.獲取info.plist檔案內的內容
//比如獲取當前版本號
NSString * currentVersion = [NSBundlemainBundle].infoDictionary[@"CFBundleVersion"];