Jer*_*ies -6 string url ios swift
我收到错误 Cannot conversion value of type string to type url in coercion at this Constant。感谢您提前的帮助:)
Run Code Online (Sandbox Code Playgroud)**let safariVC = SFSafariViewController(url: JobUrl as URL)**
import UIKit
import SafariServices
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//Indeed API Query as JSON
final let urlString = "http://api.indeed.com/ads/apisearch?publisher=9727336427429597&as_phr=&as_any=&as_not=&as_ttl=&as_cmp=&jt=parttime&st=&salary=&radius=25&l=32304&fromage=any&limit=25&sort=&psf=advsrch=&userip=1.2.3.4&useragent=Mozilla/%2F4.0%28Firefox%29&v=2&format=json"
@IBOutlet weak var tableView: UITableView!
var jobTitleArray = [String]()
var snippetArray = [String]()
var companyArray = [String]()
var cityArray = [String]()
var jobUrlArray = [String]()
// url = URL(string: epsDictionary["link"] as! String)
override func viewDidLoad() {
super.viewDidLoad()
self.downloadJsonWithURL()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//dispose of any resources that can be recreated
}
//fetch Json
func downloadJsonWithURL() {
let url = NSURL(string: urlString)
URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) ->
Void in
if let JsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)
as? NSDictionary {
print(JsonObj!.value(forKey: "results")!)
if let resultsArray = JsonObj!.value(forKey: "results") as? NSArray {
for result in resultsArray {
if let resultDict = result as? NSDictionary {
if let jobTitle = resultDict.value(forKey: "jobtitle") {
self.jobTitleArray.append(jobTitle as! String)
}
if let snippet = resultDict.value(forKey: "snippet") {
self.snippetArray.append(snippet as! String)
}
if let company = resultDict.value(forKey: "company") {
self.companyArray.append(company as! String)
}
if let city = resultDict.value(forKey: "formattedRelativeTime") {
self.cityArray.append(city as! String)
}
if let jobUrl1 = resultDict.value(forKey: "url") {
self.jobUrlArray.append(jobUrl1 as! String)
}
OperationQueue.main.addOperation({
self.tableView.reloadData()
})
}
}
}
}
}).resume()
}
func downloadJsonWithTask() {
let url = NSURL(string: urlString)
var downloadTask = URLRequest(url: (url as? URL)!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 20)
downloadTask.httpMethod = "Get"
URLSession.shared.dataTask(with: (url as? URL)!, completionHandler: {(data, response, error) ->
Void in
let jsonData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments)
print(jsonData!)
}).resume()
}
var jobToUrl = URL(string: "http://www.indeed.com/viewjob?jk=5de353a4c580dce0&qd=8FiWEXDXvmdNb_GJC9BAOpLFMiNO7rztIOPtGp_-cISTa1VWcmBigetsBoobMSCXdNyr-z6ge7UiYg2Mx15EH6m1Aj3izkOw87NHJgxznYA&indpubnum=9727336427429597&atk=1bchrhjof5hgga1k")
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
print("USER SELECTED CELL")
let JobUrl = jobUrlArray[indexPath.row]
**let safariVC = SFSafariViewController(url: JobUrl as URL)**
self.present(safariVC, animated: true, completion: nil)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return jobTitleArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
cell.jobTitle.text = jobTitleArray[indexPath.row]
cell.jobSummary.text = snippetArray[indexPath.row]
cell.employerName.text = companyArray[indexPath.row]
cell.cityName.text = cityArray[indexPath.row]
cell.ApplyButton.text = jobUrlArray[indexPath.row]
// let imageUrl = NSURL(string: imageUrlArray[indexPath.row])
// if imageUrl != nil {
// let data = NSData(contentsOf: (imageUrl as? URL)!)
// cell.imageView?.image = UIImage (data: data as! Data)
// }
return cell
}
}
Run Code Online (Sandbox Code Playgroud)
您无法将字符串转换为 URL。投射只是说“好吧,这个盒子里的东西。它不是一个字符串,它是一个 URL。” 如果该对象不能兼作其他类,则转换失败。
您需要使用字符串作为输入来创建 URL:
let url = URL(string: myString)
Run Code Online (Sandbox Code Playgroud)