我是iOS新手。我从plist文件中设置了webview中的url。下面的代码是客观的C.
Swift会有什么相同的东西?`
NSString *path = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
[_webView loadRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[dict objectForKey:@"webViewURL"]]]];`
答案 0 :(得分:1)
试试这个,
let plistPath = NSBundle.mainBundle().pathForResource("YourPlistName", ofType: "plist")
let dict = NSDictionary(contentsOfFile: plistPath!)
webview.loadRequest(NSURLRequest(URL: NSURL(string: dict?.objectForKey("webViewURL") as! String)!))
答案 1 :(得分:1)
以下是等效代码:
var webview: UIWebView?
func loadwebView() {
if let plist = NSBundle.mainBundle().pathForResource("Settings", ofType: "plist"),
let plistDict = NSDictionary(contentsOfFile: plist),
let urlString = plistDict.objectForKey("webViewURL") as? String,
let url = NSURL(string: urlString) {
webview?.loadRequest(NSURLRequest(URL: url))
}
}