0
這裏的代碼將歸功於Jameson Quave。類型「字符集」沒有成員「utf8」
網址:www.jamesonquave.com/blog/developing-ios-apps-using-swift-tutorial-part-2/
我試圖編輯與斯威夫特3.我的問題而努力有針對該行的錯誤消息:
if let escapedSearchTerm = itunesSearchTerm.addingPercentEncoding(withAllowedCharacters: .urlquery)
(它指出.utf8
代碼)
我不確定什麼,我需要在.urlquery
部分放置
我得到的錯誤代碼是標題。我試圖谷歌的答案,發現String.Encoding.utf8
哪些也沒有工作。原始代碼有NSUTF8StringEncoding
。
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var appsViewTable: UITableView!
var tableData = []
func searchItunesFor(searchTerm: String) {
//The iTunes API wants multiple terms seperated by + symbols, so replace spaces with + signs
let itunesSearchTerm = searchTerm.replacingOccurrences(of: " ", with: "+", options: NSString.CompareOptions.caseInsensitive, range: nil)
//Now escape anything else that isn't URL-friendly
if let escapedSearchTerm = itunesSearchTerm.addingPercentEncoding(withAllowedCharacters: .utf8) {
let urlPath = "http://itunes.apple.com/search?term=\(escapedSearchTerm)&media=software"
let url = NSURL(string: urlPath)
let session = URLSession.shared
let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in
printIn("Task completed")
if(error != nil) {
// If there is an error in the web request, print it to the console
printIn(error.localizedDescription)
}
var err: NSError?
if let jsonResult = NSJSONSerialization.JSONObjectiveWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSDictionary {
if(err != nil) {
// If there is an error parsing JSON, print it to the console
printIn("JSON Error \(err!.localizedDescription)")
}
if let results: NSArray = jsonResult["results"] as? NSArray {
dispatch_async(dispatch_get_main_queue(), {
self.tableData = results
self.appsTableView!.reloadData()
})
}
}
})
// The task if just an object with all these properties set
// In order to actually make the web request, we need to "resume"
task.resume()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// 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.
}
}
嘗試使用'String.Encoding.utf8.rawValue' – Tj3n