我有2个类。 在第一课我有一个标签和一个按钮,在第二课我有JsonPars功能lable.text - 致命错误
我需要写第二类的数据到第一类的标签。
这里是我的代码:
import UIKit
import CoreLocation
//main class
class ViewController: UIViewController{
@IBOutlet weak var labl: UILabel!
@IBAction func btn(sender: AnyObject) {
Json().Pars()
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
和第二类:
import Foundation
//Json-class
class Json {
func Pars() {
let url = NSURL(string: "http://my.url.com")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in
if (error != nil){
println(error.localizedDescription)
}
var err: NSError?
let parseObj: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &err)
if let json = parseObj as? NSDictionary{
if let response = json["response"] as? NSDictionary{
if let obj = response["Object"] as? NSDictionary{
if let data = obj["data"] as? NSString {
println(data)//work's and i have a data
ViewController().labl.text = data //fatal error: unexpectedly found nil while unwrapping an Optional value
}
}
}
}
})
task.resume()
}
}
对不起FO我的英语
请将您的示例代码缩短为仅包含理解问题所需的内容。 –