0

嗨,我是目标 c 的新手。

我将我的 URL 数据保存在资源下的 plist 文件中。这是我的代码。

NSString *plistPath =[[[NSBundle mainBundle] resourcePath] 
           stringByAppendingPathComponent:@"myfile.plist"];

NSDictionary *plistDict = [[NSUserDefaults standardUserDefaults]
                                       dictionaryRepresentation];

if([plistDict writeToFile:plistPath atomically:YES]) {
    NSLog(@"success");
} else {
    NSLog(@"error");
}

现在我想从保存的 plist(我的 file.plist)中检索 URL,我如何检索这个我可以使用该 url 进行 web 视图。

例如:

urlString.stringValue = ????

在这里,我正在使用urlString.stringValuewebview。

[[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:
                [NSURL URLWithString:urlString.stringValue]]];

谢谢

4

1 回答 1

0

首先,您不需要自己将 NSUserDefaults 保存到文件中。它的内容会定期自动保存,或者您可以synchronize立即调用保存。

但是,如果您想将字典保存到 plist,请使用writeToFile:atomically:您已有的方法。然后您可以通过使用 plist 的内容创建一个新字典来再次加载它:

[NSDictionary dictionaryWithContentsOfFile:plistPath];
于 2013-04-05T14:01:57.530 回答