我正在实现基于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中获取字典对象的问题iphone sdk
- 3. 如何从iphone sdk中随机获取数据库中的值?
- 4. 读取的plist iPhone SDK的
- 5. 如何从iPhone sdk中的标记获取文本字段值?
- 6. iPhone从plist获取数据
- 7. 如何从CandleStick中获取价值并添加到LineChart - > MPANDROIDCHART
- 8. 当从json获取数据并在plist中存储时,为plist获得null值
- 9. 如何读取plist数据并从中获取int?
- 10. 如何存储Plist中的值并在iphone 3.0中运行时读取?
- 11. iPhone SDK:如何在警报中获取UITextField的值?
- 12. 如何添加NSDebug.h并在iPhone SDK中使用NSZombie
- 13. 从对象中获取价值,并在JComboBox中添加
- 14. 如何将文件添加到xcode iphone项目中的.plist?
- 15. 如何将plist文件添加到xcode中的iPhone/iPod设备?
- 16. 从plist获取数据到NSMutableArray并将NSMutableArray写入到相同的plist iPhone
- 17. 如何从iphone sdk中的ISOCountryCode获取ISOCurrencyCode?
- 18. iphone sdk如何从照片库中获取完整的图像?
- 19. 如何从数组中获取字段值并使用Typescript添加值
- 20. 从plist值的GSEventSetBacklightLevel iPhone
- 21. 如何从corona SDK中的文件读取/获取值?
- 22. iphone sdk:如何将事件添加到UIButton UIWebView中添加
- 23. 从Facebook SDK中的strings.xml获取价值
- 24. 如何在iphone中获取iphone的最后拒绝数量sdk
- 25. 如何读写objective-c中的plist文件? (iPhone SDK)
- 26. 在iPhone中更新值plist
- 27. 从数据库中获取最大值并添加一个
- 28. 将控制添加到TreeNode并从中获取值
- 29. 如何从数据库中获取布尔值并将其添加到TextView?
- 30. 创建一个plist iPhone SDK
可能的重复[从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