我正在實現基於Web服務的應用程序。因爲我需要在.plist中添加一個字符串作爲屬性,並且我需要從代碼中獲取.plist中的值。你們能幫我解決嗎?如何添加並從iPhone中的.plist中獲取值sdk
18
A
回答
29
下面是一個代碼示例:
NSString *path = [[NSBundle mainBundle] pathForResource: @"YourPLIST" ofType: @"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: path];
id obj = [dict objectForKey: @"YourKey"];
15
NSBundle* mainBundle = [NSBundle mainBundle];
// Reads the value of the custom key I added to the Info.plist
NSString *value = [mainBundle objectForInfoDictionaryKey:@"key"];
//Log the value
NSLog(@"Value = %@", value);
// Get the value for the "Bundle version" from the Info.plist
[mainBundle objectForInfoDictionaryKey:@"CFBundleVersion"];
// Get the bundle identifier
[mainBundle bundleIdentifier];
5
NSURL *url = [[NSBundle mainBundle] URLForResource:@"YOURPLIST" withExtension:@"plist"];
NSArray *playDictionariesArray = [[NSArray alloc ] initWithContentsOfURL:url];
NSLog(@"Here is the Dict %@",playDictionariesArray);
或者您可以使用下面還
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Sample.plist"];
2
從plist得到很簡單。
NSString *path = [[NSBundle mainBundle] pathForResource:@"SaveTags" ofType:@"plist"];
if (path) {
NSDictionary *root = [NSDictionary dictionaryWithContentsOfFile:path];
}
如果你想要的東西添加到plist中,也許你可以在這裏找到答案: How to write data in plist?
但是,如果你只是想在你節省一些消息應用程序,是NSUserDefaults的更好的方法。
0
你不能這樣做。無論是iOS還是Mac OS,任何Bundle都是隻讀的,只能讀取它,並且不能創建文件,編寫文件或對文件進行任何操作。這是蘋果的安全功能的一部分。你可以使用NSDocumentsDirectory來爲你的應用程序編寫並閱讀你需要的東西
相關問題
- 1. 如何從Android中的plist獲取值?
- 2. 如何從分層.plist中獲取值?
- 3. 從plist中獲取字典對象的問題iphone sdk
- 4. 如何從iphone sdk中隨機獲取數據庫中的值?
- 5. 讀取的plist iPhone SDK的
- 6. 如何從iPhone sdk中的標記獲取文本字段值?
- 7. iPhone從plist獲取數據
- 8. 以編程方式將plist添加到plist(iphone sdk)
- 9. 如何從CandleStick中獲取價值並添加到LineChart - > MPANDROIDCHART
- 10. 當從json獲取數據並在plist中存儲時,爲plist獲得null值
- 11. 如何讀取plist數據並從中獲取int?
- 12. 如何從plist中加載值?
- 13. 如何存儲Plist中的值並在iphone 3.0中運行時讀取?
- 14. iPhone SDK:如何在警報中獲取UITextField的值?
- 15. 如何添加NSDebug.h並在iPhone SDK中使用NSZombie
- 16. 如何將plist文件添加到xcode中的iPhone/iPod設備?
- 17. 如何將文件添加到xcode iphone項目中的.plist?
- 18. 從plist值的GSEventSetBacklightLevel iPhone
- 19. iphone sdk如何從照片庫中獲取完整的圖像?
- 20. 如何從iphone sdk中的ISOCountryCode獲取ISOCurrencyCode?
- 21. 從plist獲取數據到NSMutableArray並將NSMutableArray寫入到相同的plist iPhone
- 22. 從對象中獲取價值,並在JComboBox中添加
- 23. 如何從查詢中獲取值並在Rails上添加值爲1
- 24. 如何從數組中獲取字段值並使用Typescript添加值
- 25. iphone sdk:如何將事件添加到UIButton UIWebView中添加
- 26. 從XML獲取特定的節點值並添加到ArrayList中
- 27. 如何從corona SDK中的文件讀取/獲取值?
- 28. 如何在iphone中獲取iphone的最後拒絕數量sdk
- 29. 從Facebook SDK中的strings.xml獲取價值
- 30. 如何在iPhone中添加和閱讀項目到plist?
可能的重複[從plist獲取數據到NSMutableArray並將NSMutableArray寫入同一個plist iPhone](http://stackoverflow.com/questions/9193363/getting-data-from-plist-to-nsmutablearray-and-writing-the-nsmutablearray-to-same) – Monolo 2012-07-15 10:22:44
請參閱[此鏈接](http://www.iphoneexamples.com/)。希望這會對你非常有幫助爲你。祝一切順利。 – Warrior 2010-10-13 12:54:45