我在下面的代码中遇到编译器错误。我已经导入了所需的基础库,所以我不确定到底出了什么问题,请有人建议我可能做错了什么或者我可能会做些什么来解决这个问题?
由于
import UIKit
import Foundation
class ViewController: UIViewController {
@IBOutlet var areaTxt: UITextField!
@IBOutlet var resultLbl: UILabel!
@IBAction func goBtn(sender: AnyObject) {
if(countElements(areaTxt.text) > 0){
var removedSpaces = areaTxt.text.stringByReplacingOccurrencesOfString(" ", withString: "");
var url = "http://weather-forecast.com/locations/" + removedSpaces + "/forecasts/latest";
self.getWeatherReportForArea(url);
}else{
let alert = UIAlertView()
alert.title = "Alert"
alert.message = "The user location is empty"
alert.addButtonWithTitle("Ok")
alert.show()
}
}
func getWeatherReportForArea(area: String){
var url = NSURL(string: area);
let task = NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in
var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding);
if(urlContent.bridgeToObjectiveC().containsString("<span class=\"phrase\">")){
var contentArray = urlContent.componentsSeparatedByString("<span class=\"phrase\">");
var newContentArray = contentArray[1].componentsSeparatedByString("</span>");
var value = newContentArray[0].stringByReplacingOccurrencesOfString("°", withString: "º");
dispatch_async(dispatch_get_main_queue()){
self.resultLbl.text = value;
}
}else{
}
}
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.
}
}
答案 0 :(得分:6)
bridgeToObjectiveC()
。现在,桥接是透明的,只需使用
if urlContent.containsString("<span class=\"phrase\">") {
...
}