K
K
Kennedy_SK2015-05-21 17:55:34
Programming
Kennedy_SK, 2015-05-21 17:55:34

Inheritance in Swift. How to display data on a form?

Good day!
There are 2 classes, I need to write the coordinates to the lat and log variables in the ViewController class (main class) in order to use them in other classes (as an example, create a class with global variables and write all variables there), and what would I could access them from other classes.
In the second class, Json class (Json-class), I need the result of parsing, in this example, data is written to the label on the main class, but it hits a critical error: fatal
error: unexpectedly found nil while unwrapping an Optional value
write down the data value - so that I can access other classes (you can use the same class with global variables)
Tell me how I can implement all this
Thank you in advance!)

import UIKit
import CoreLocation
 
//main class
 
class ViewController: UIViewController, CLLocationManagerDelegate{
   
    
    @IBOutlet weak var labl: UILabel!
 
    @IBAction func btn(sender: AnyObject) {
        labl.text = Json().mess
    }
    var locationManager = CLLocationManager()
    
    var lat: Double?
    var log: Double?
 
 
    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
        Json().Pars()
        }
    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        var userLocation:CLLocation = locations [0] as! CLLocation
        
        locationManager.stopUpdatingLocation()
        let location = CLLocationCoordinate2D (latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
        println(userLocation.coordinate.longitude)
        println(userLocation.coordinate.latitude)
        lat = userLocation.coordinate.latitude
        log = userLocation.coordinate.longitude
    }
    
    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
        println("error")
    }
 
    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{
               f let obj = response["Object"] as? NSDictionary{
                    if let data = obj["data"] as? NSString {
                       println(data)//вывод данных
                       ViewController().labl.text = data //на этом моменте бьет критическую ошибку fatal error: unexpectedly found nil while unwrapping an Optional value
 
           
}
}
}
        }
           
    })
 
        task.resume()
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav H, 2015-05-21
@shomishinec

Leading question:
ViewController()what does it do?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question