IOS之NSBundle方法詳解

原文地址:http://blog.csdn.net/shx_yi/article/details/8755415?

1辐董、使用類方法創(chuàng)建一個NSBundler對象

+ (NSBundle *)mainBundle;

eg:[NSBundle mainBundle];

2放闺、使用路徑獲取一個NSBundle 對象,這個路徑應該是一個目錄的全路徑

+ (NSBundle *)bundleWithPath:(NSString *)path;

eg:

NSString *path = [mailBundle resourcePath];

NSBundle *language = [NSBundlebundleWithPath:path];

3三圆、使用路徑初始化一個NSBundle

- (id)initWithPath:(NSString *)path;

4骂租、使用一個url 創(chuàng)建并初始化一個NSBundle對象(這是一個類方法)

注:這里的url 是一個特殊的 文件url路徑

+ (NSBundle *)bundleWithURL:(NSURL *)url

5蚊夫、使用一個url 初始化一個NSBundle對象

注:這里的url 是一個特殊的 文件url路徑

- (id)initWithURL:(NSURL *)url

6券时、根據(jù)一個特殊的class 獲取NSBundle

+ (NSBundle *)bundleForClass:(Class)aClass;

eg:根據(jù)當前的class 獲取一個NSBundle // 獲取當前類的NSBundle

NSBundle *bud = [NSBundle bundleForClass:[self class]];

NSLog(@"bud==%@",bud);

輸出結果如下:

NSBundle

7邮旷、獲取特定名稱的bundle

+ (NSBundle *)bundleWithIdentifier:(NSString *)identifier;

8黄选、使用NSBundle 獲取所有的bundle信息(由于ios安全沙盒的限制,所有的獲取的資源婶肩,是應用程序的資源)

注:官方標注办陷,獲取所有的非framework 的bundle;

+ (NSArray *)allBundles;

eg:

NSArray *array = [NSBundle allBundles];

NSLog(@"array===%@",array);

打印的結果如下:

array===(

"NSBundle (loaded)"

)

9、獲取應用程序加載的所有framework的資源律歼,

+ (NSArray *)allFrameworks;

eg:

NSArray *array = [NSBundle allFrameworks];

NSLog(@"%@",array);

輸出的結果如下:(

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)",

"NSBundle (loaded)"

)

10民镜、判斷bundle 加載,(按照官方文檔,You don’t need to load a bundle’s executablecode to search the bundle’s resources.我們不需要調(diào)用這個方法,)

- (BOOL)load;

11险毁、判斷bundle 加載

- (BOOL)isLoaded;

12制圈、判斷bundle 加載

- (BOOL)unload;

13、加載資源畔况,如果有錯誤的話鲸鹦,會放置錯誤信息

- (BOOL)preflightAndReturnError:(NSError **)error

- (BOOL)loadAndReturnError:(NSError **)error

14、獲取bundle 類實例的 url

- (NSURL *)bundleURL

15跷跪、獲取bundle 類實例的 resourceUrl 資源

- (NSURL *)resourceURL

16馋嗜、獲取bundle 類實例的 可執(zhí)行的URL

- (NSURL *)executableURL

17、(Returns the file URL of the executable with the specified namein the receiver’s bundle.返回一個,文件的URL吵瞻,使用一個特殊的名稱)

- (NSURL *)URLForAuxiliaryExecutable:(NSString*)executableName

18葛菇、獲取當前NSBundle實例的 URL 資源

- (NSURL *)privateFrameworksURL

19甘磨、獲取共享的frameworkdURL

- (NSURL *)sharedFrameworksURL

20、 獲取支持的Bundle的Url

- (NSURL *)sharedSupportURL

21眯停、獲取添加插件的URL

- (NSURL *)builtInPlugInsURL

// 已經(jīng)不能使用宽档,

- (NSURL *)appStoreReceiptURL

22、 獲取bundle 的path 路徑

- (NSString *)bundlePath;

23庵朝、獲取bundle 的資源路徑字符串

- (NSString *)resourcePath;

24、獲取bundle 可執(zhí)行文件路徑

- (NSString *)executablePath;

25又厉、獲取bundle 輔助的path

- (NSString *)pathForAuxiliaryExecutable:(NSString*)executableName;

26九府、獲取私有的路徑框架

- (NSString *)privateFrameworksPath;

27、獲取共享的framework path 路徑

- (NSString *)sharedFrameworksPath;

28覆致、獲取共享的路徑

- (NSString *)sharedSupportPath;

29侄旬、獲取插件的路徑

- (NSString *)builtInPlugInsPath;

// 已經(jīng)廢棄,不能調(diào)用

+ (NSURL *)URLForResource:(NSString *)name withExtension:(NSString*)ext subdirectory:(NSString *)subpath inBundleWithURL:(NSURL*)bundleURL

// 已經(jīng)廢棄

+ (NSArray *)URLsForResourcesWithExtension:(NSString *)extsubdirectory:(NSString *)subpath inBundleWithURL:(NSURL*)bundleURL

30煌妈、使用bundle 創(chuàng)建一個資源文件的URL

- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString*)ext

eg:

NSURL* URL=[[NSBundle mainBundle] URLForResource:fileNamewithExtension:@"png"];

31儡羔、(官方描述如下:Returns the file URL for the resource file identified bythe specified name and extension and residing in a given bundledirectory.

使用資源文件的名稱以及擴展名,還有子路徑)

- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString*)ext subdirectory:(NSString *)subpath

32璧诵、(官方描述:

Returns the file URL for the resource identified by the specifiedname and file extension,

located in the specified bundle subdirectory, and limited to globalresources and those associated with the specifiedlocalization.)

同上一個方法汰蜘,不同的是添加了本地資源文件的信息

- (NSURL *)URLForResource:(NSString *)name withExtension:(NSString*)ext subdirectory:(NSString *)subpath localization:(NSString*)localizationName

33、根據(jù)文件的后綴名稱和子目錄之宿,獲取一個NSURL 的數(shù)組

- (NSArray *)URLsForResourcesWithExtension:(NSString *)extsubdirectory:(NSString *)subpath

34族操、同上面的方法,添加了本地化的一個資源文件

- (NSArray *)URLsForResourcesWithExtension:(NSString *)extsubdirectory:(NSString *)subpath localization:(NSString*)localizationName

35比被、根據(jù)資源文件的名稱色难,或者是文件的后綴名稱以及目錄的路徑,獲取 path

+ (NSString *)pathForResource:(NSString *)name ofType:(NSString*)ext inDirectory:(NSString *)bundlePath;

36等缀、根據(jù)文件的擴展名枷莉,以及資源的路徑,獲取一個數(shù)組

+ (NSArray *)pathsForResourcesOfType:(NSString *)extinDirectory:(NSString *)bundlePath;

37尺迂、根據(jù)文件的名稱和擴展名獲取 path 名稱

- (NSString *)pathForResource:(NSString *)name ofType:(NSString*)ext;

37笤妙、根據(jù)文件的名稱和擴展名獲取 path 名稱

- (NSString *)pathForResource:(NSString *)name ofType:(NSString*)ext inDirectory:(NSString *)subpath;

37、根據(jù)文件的名稱和擴展名獲取 path 名稱

- (NSString *)pathForResource:(NSString *)name ofType:(NSString*)ext inDirectory:(NSString *)subpath forLocalization:(NSString*)localizationName;

38噪裕、根據(jù)文件的擴展名和子目錄獲取一個資源文件的數(shù)組

- (NSArray *)pathsForResourcesOfType:(NSString *)extinDirectory:(NSString *)subpath;

39危喉、同上,添加了資源文件的一個路徑

- (NSArray *)pathsForResourcesOfType:(NSString *)extinDirectory:(NSString *)subpath forLocalization:(NSString*)localizationName;

40州疾、方法調(diào)用解釋如下

- (NSString *)localizedStringForKey:(NSString *)key value:(NSString*)value table:(NSString *)tableName NS_FORMAT_ARGUMENT(1);

你可以通過NSBundle來找到對應key值的vaule.

NSBundle *main = [NSBundle mainBundle];

NSString *aString = [main localizedStringForKey:@"Key1"

value:@"DefaultValue1"

table:@"Find"];

上面的代碼會在Find.strings中查找"Key1"對應的 vuale字符串.如果沒有提供用戶指定的語言的本地化資源,那么就會查找第二個所選語言,如果第二個也沒有本地化資源,就依次找下去.如果到最后還是沒有找到,那么 ""DefaultValue1"將會返回

// 返回當前bundle的唯一標示:(即:應用的唯一標示)

- (NSString *)bundleIdentifier;

eg:com.company.ios-Example-NSBundle.IOS-Example-NSBundle

// 獲取資源文件的dictionary 對象

- (NSDictionary *)infoDictionary;

eg:

{

CFBundleDevelopmentRegion =en;

CFBundleDisplayName ="IOS_Example_NSBundle";

CFBundleExecutable ="IOS_Example_NSBundle";

CFBundleExecutablePath ="/Users/ctrip1/Library/Application Support/iPhoneSimulator/6.1/Applications/2F3DA58F-5CF9-48A2-ADB2-C923A29B519E/IOS_Example_NSBundle.app/IOS_Example_NSBundle";

CFBundleIdentifier ="com.company.ios-Example-NSBundle.IOS-Example-NSBundle";

CFBundleInfoDictionaryVersion= "6.0";

CFBundleInfoPlistURL ="Info.plist -- file://localhost/Users/ctrip1/Library/ApplicationSupport/iPhoneSimulator/6.1/Applications/2F3DA58F-5CF9-48A2-ADB2-C923A29B519E/IOS_Example_NSBundle.app/";

CFBundleName ="IOS_Example_NSBundle";

CFBundlePackageType =APPL;

CFBundleShortVersionString ="1.0";

CFBundleSignature ="????";

CFBundleSupportedPlatforms =? ? (

iPhoneSimulator

);

CFBundleVersion = "1.0";

DTPlatformName =iphonesimulator;

DTSDKName ="iphonesimulator6.1";

LSRequiresIPhoneOS = 1;

NSBundleInitialPath ="/Users/ctrip1/Library/Application Support/iPhoneSimulator/6.1/Applications/2F3DA58F-5CF9-48A2-ADB2-C923A29B519E/IOS_Example_NSBundle.app";

NSBundleResolvedPath ="/Users/ctrip1/Library/Application Support/iPhoneSimulator/6.1/Applications/2F3DA58F-5CF9-48A2-ADB2-C923A29B519E/IOS_Example_NSBundle.app";

UIDeviceFamily =? ? (

1,

2

);

UIRequiredDeviceCapabilities =? ? (

armv7

);

UISupportedInterfaceOrientations =? ? (

UIInterfaceOrientationPortrait,

UIInterfaceOrientationLandscapeLeft,

UIInterfaceOrientationLandscapeRight

);

}

41辜限、返回本地化資源的NSDictionary 對象

- (NSDictionary *)localizedInfoDictionary;

42、根據(jù)key 值獲取本地化資源對象的值

- (id)objectForInfoDictionaryKey:(NSString *)key;

43严蓖、(官方描述:Returns the Class object for the specifiedname.根據(jù)類名字符串獲取一個類對象)

- (Class)classNamed:(NSString *)className;

44薄嫡、返回主要的類

- (Class)principalClass;

45氧急、返回本地化資源的列表

- (NSArray *)localizations;

46、本地化的語言列表

- (NSArray *)preferredLocalizations;

47毫深、使用創(chuàng)建的類獲取本地化語言

- (NSString *)developmentLocalization;

48吩坝、(官方描述 Returns one or more localizations from the specified listthat a bundle object would use to locate resources for the currentuser.)

+ (NSArray *)preferredLocalizationsFromArray:(NSArray*)localizationsArray;

+ (NSArray *)preferredLocalizationsFromArray:(NSArray*)localizationsArray forPreferences:(NSArray *)preferencesArray;

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市哑蔫,隨后出現(xiàn)的幾起案子钉寝,更是在濱河造成了極大的恐慌,老刑警劉巖闸迷,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件嵌纲,死亡現(xiàn)場離奇詭異,居然都是意外死亡腥沽,警方通過查閱死者的電腦和手機逮走,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來今阳,“玉大人师溅,你說我怎么就攤上這事《苌啵” “怎么了墓臭?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長妖谴。 經(jīng)常有香客問我起便,道長,這世上最難降的妖魔是什么窖维? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任榆综,我火速辦了婚禮,結果婚禮上铸史,老公的妹妹穿的比我還像新娘鼻疮。我一直安慰自己,他們只是感情好琳轿,可當我...
    茶點故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布判沟。 她就那樣靜靜地躺著,像睡著了一般崭篡。 火紅的嫁衣襯著肌膚如雪挪哄。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天琉闪,我揣著相機與錄音迹炼,去河邊找鬼。 笑死,一個胖子當著我的面吹牛斯入,可吹牛的內(nèi)容都是我干的砂碉。 我是一名探鬼主播,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼刻两,長吁一口氣:“原來是場噩夢啊……” “哼增蹭!你這毒婦竟也來了?” 一聲冷哼從身側響起磅摹,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤滋迈,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后户誓,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體饼灿,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年厅克,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片橙依。...
    茶點故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡证舟,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出窗骑,到底是詐尸還是另有隱情女责,我是刑警寧澤,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布创译,位于F島的核電站抵知,受9級特大地震影響,放射性物質發(fā)生泄漏软族。R本人自食惡果不足惜刷喜,卻給世界環(huán)境...
    茶點故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望立砸。 院中可真熱鬧掖疮,春花似錦、人聲如沸颗祝。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽螺戳。三九已至搁宾,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間倔幼,已是汗流浹背盖腿。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留损同,地道東北人奸忽。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓堕伪,卻偏偏與公主長得像,于是被迫代替她去往敵國和親栗菜。 傳聞我的和親對象是個殘疾皇子欠雌,可洞房花燭夜當晚...
    茶點故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內(nèi)容