簡體   English   中英

無法從mainbundle中讀取plist

[英]Can not read plist from mainbundle

我遇到一些奇怪的問題。

我無法從應用程序mainbundle中讀取plist文件

該文件位於“副本捆綁資源”中

但以某種方式無法讀取

這是代碼

NSLog(@"%@",[[NSBundle mainBundle] pathForResource:@"8chk" ofType:@"plist"]);

它一直打印(null)

這是屏幕截圖

在此處輸入圖片說明

誰能告訴我發生了什么事?

恐怕要說的是,您沒有將此8chk.plist添加到目標中。

再次將plist文件拖放到項目中,確保在將文件添加到項目中時說“復制ProjectName中的文件”時選中復選框

然后使用此代碼使其可訪問並從plist讀取值

NSString *path = [[NSBundle mainBundle] pathForResource: @"YourPLIST" ofType: @"plist"]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: path];
[id obj = [dict objectForKey: @"YourKey"]; 

要么

// Reads the value of the custom key you added to the Info.plist
NSString *value = [mainBundle objectForInfoDictionaryKey:@"key"];

//Log the value
NSLog(@"Value = %@", value);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Info.plist"];

這可能有效。 確保將plist復制到目標並放在根目錄中。也可以嘗試將plist文件放在應用默認plist保留的位置。

:)

嗨Shoeb Amin,請檢查您的plist文件是否已在Target-> Copy Bundle資源中添加

您的路線是錯誤的。 如果項目導航器顯示正確的樹,則copy bundle resource應僅讀取copy bundle resource 8chk.plist而不是8chk.plist ..in resources/8chk

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM