Bis*_*128 1 objective-c ios swift
我在下面的代码中收到编译器错误.我已经导入了所需的基础库,所以我不确定到底出了什么问题,请有人建议我可能做错了什么或者我可能会做些什么来解决这个问题?
谢谢
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.
}
}
Run Code Online (Sandbox Code Playgroud)
bridgeToObjectiveC() 在Xcode 6 Beta 5中删除了.现在桥接是透明的,只需使用
if urlContent.containsString("<span class=\"phrase\">") {
...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
987 次 |
| 最近记录: |