我正在使用 iTunes 搜索 API,为了进行测试,我正在搜索“愤怒的小鸟”并打印结果。但是当我将其转换urlPath
为 NSURL 时,我得到了一个nil
值。我该如何解决?有urlPath
链接,但它nil
在 url 变量中。这是确切的错误:
致命错误:在展开可选值时意外发现 nil。
var searchTerm: String = "Angry Birds"
var itunesSearchTerm = searchTerm.stringByReplacingOccurrencesOfString(" ", withString: "+", options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil)
var escapedSearchTerm = itunesSearchTerm.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
var urlPath = "https://itunes.apple.com/search?term=\(escapedSearchTerm)&media=software"
var url: NSURL = NSURL(string: urlPath)!
var request: NSURLRequest = NSURLRequest(URL: url)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)!
print("Search iTunes API at URL \(url)")
connection.start()