在UI标签上显示字符串

时间:2014-12-14 13:41:57

标签: ios xcode6

我是Swift的新手,在Obj-C中也不是很好。我正在尝试向UILabel显示一条消息,但是在这方面遇到了困难。

我迫使一个" AnyObject"从NSString获取网页的html数据(存储在url变量中)。我将这个对象拆分成一个双元素数组,其中代码中给出了分隔字符串。

然后我进一步将数组的第二个元素拆分为2个元素,现在我想在UILabel上显示这个最新数组的第一个元素(名为" message")。

这是我的代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!

    var city: String = "city" // just a dummy initialization   
    var web: String = "http://www.weather-forecast.com/locations/"   

    @IBOutlet weak var message: UILabel! 
    @IBAction func getWeather(sender: AnyObject) {         
        city = textField.text;
        city.stringByReplacingOccurrencesOfString(" ", withString: "")
        city = city + "/forecasts/latest"
        web = web + city;

        var url = NSURL(string: web)
       // println(url)

        let task = NSURLSession.sharedSession().dataTaskWithURL(url!){ (data, response, error) in

            var content = NSString(data: data, encoding: NSUTF8StringEncoding);
            var splitArray = content?.componentsSeparatedByString("<span class=\"phrase\">");

            content = (splitArray![1]) as? NSString
            splitArray = content?.componentsSeparatedByString("</span>") 
            var weather = splitArray![1] as String
            message.text = weather;        
        }
        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.
    }
}

我在最后一行message.text = weather收到错误,错误消息显示为Reference to property'message' in closure requires explicit 'self'. to make capture semantics explicit

有人可以帮忙描述一下这个错误信息的含义吗?添加&#39; self&#39;之前的消息&#39;不会在标签中显示所需的字符串。

谢谢!

0 个答案:

没有答案