2013-04-04 77 views
3

我有一个名为“config.plist”config.plist在xcode中的plist,我试图检索注册网址(键:reg),但是当我打印结果时显示空值?如何从plist中检索字符串?

这是我的代码来检索字符串..请参考我的plist结构图像。

NSMutableURLRequest *request =[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString: [[_config objectForKey:@"URL"] objectForKey:@"reg"]]]; 
+0

你在哪里存储你的plist文件? – 2013-04-04 05:53:23

+0

更新:它在我的xcode – akh 2013-04-04 06:06:43

+0

什么是Xcode?这是一个IDE,根本不存储任何plist。 – 2013-04-04 06:07:38

回答

1

试试这个代码

NSString *plistFilePath=[[NSBundle mainBundle] pathForResource:@"config" ofType:@"plist"]; 
NSDictionary *urlDictionary=[[NSDictionary alloc] initWithContentsOfFile:plistFilePath]; 
NSString *urlString=[[urlDictionary objectForKey:@"URL"] valueForKey:@"reg"]; 
2

尝试

NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"config.plist"]; 
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:path]; 
NSString *result = [[[dictionary objectForKey:@"Root"] objectForKey:@"URL"]objectForKey:@"reg"]; 

编辑的资源:

NSString *path = [[NSBundle mainBundle] pathForResource:@"config" ofType:@"plist"]; 
+0

+1 ...但是你可以使用keypath'来提高可读性 – 2013-04-04 05:57:19

+0

还是它的空值 – akh 2013-04-04 05:57:49

+0

如果它存储在文档中,看看Bhargavi如何编辑我的答案 – 2013-04-04 06:00:01

4
NSString *plist=[[NSBundle mainBundle] pathForResource:@"config" ofType:@"plist"]; 
NSDictionary *dict=[[NSDictionary alloc] initWithContentsOfFile:plist]; 
NSString *stringValue=dict[@"URL"][@"reg"]; 

编辑:非常重要的,了解以下信息:

在上面dict本身就是objectForKey:@"Root"

和I /我们试图通过[@"Root"][@"URL"][@"reg"]发现这是不存在的。

因此,对于具有namingConventions可读性(什么店)应该是:

NSString *plist=[[NSBundle mainBundle] pathForResource:@"config" ofType:@"plist"]; 
NSDictionary *rootDict=[[NSDictionary alloc] initWithContentsOfFile:plist]; 
NSString *stringValue=dict[@"URL"][@"reg"]; 

或者,甚至

NSString *plist=[[NSBundle mainBundle] pathForResource:@"config" ofType:@"plist"]; 
NSString *stringValue=[[NSDictionary alloc] initWithContentsOfFile:plist][@"URL"][@"reg"]; 
+0

IT也是空值 – akh 2013-04-04 06:00:38

+0

我看到您的更新...按照Bhargavi在乔治的回答中建议的路径 – 2013-04-04 06:02:15

+0

这是因为您评论说它位于文档目录中,请查看编辑在顶部 – 2013-04-04 06:02:29

1
NSString *plist=[[NSBundle mainBundle] pathForResource:@"config" ofType:@"plist"]; 
    NSLog(@"path%@",plist); 
    NSDictionary *dict=[[NSDictionary alloc] initWithContentsOfFile:plist]; 
    NSLog(@"d%@",dict); 
    NSString *stringValue=[[dict objectForKey:@"URL"] valueForKey:@"reg"]; 

这件事适用于me..Thanks您所有帮助

+0

邮寄plist文件给我,如果没有问题的发送。在个人资料中找到我的邮件ID – 2013-04-04 06:33:47

相关问题