类型“字符集”没有成员“utf8”

时间:2017-01-10 08:42:25

标签: ios swift utf-8

这里的代码归功于Jameson Quave。

网址:www.jamesonquave.com/blog/developing-ios-apps-using-swift-tutorial-part-2/

我尝试编辑它以使用Swift 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.
    }
}

1 个答案:

答案 0 :(得分:1)

您需要使用.block { display:table; height:400px; } .block__module { display:table-cell; vertical-align:middle; } .block__bg-1, .block__bg-2 { height:400px; width:50px; background: url("https://upload.wikimedia.org/wikipedia/commons/thumb/6/68/GullBraceLeft.svg/2000px-GullBraceLeft.svg.png") no-repeat center; background-size:contain; } .block__bg-2 { transform: scaleX(-1); }

.urlQueryAllowed

withAllowedCharacters 需要一个字符集,用于定义所有不需要转义的字符。它与文本编码无关(例如UTF-8)。