如何从plist中检索字符串?

时间:2013-04-04 05:50:47

标签: ios nsarray plist nsdictionary

我在xcode中有一个名为“config.plist”config.plist的plist,我试图检索寄存器url(key:reg)但是当我打印出那个显示空值的结果时?

这是我的代码,用于检索字符串..请为我的plist结构引用图片。

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

4 个答案:

答案 0 :(得分:4)

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

编辑:了解以下内容非常重要:

上述dict本身就是objectForKey:@"Root"

而我/我们试图通过[@"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"];

答案 1 :(得分: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"];

答案 2 :(得分: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"];

这件事对我有用。谢谢你的帮助

答案 3 :(得分:1)

试用此代码

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