Reputation: 535
I am having a problem with a JSON library I am using for an iOS 9.0 project, on Xcode 7 beta 5, running on OS X El Cap.
I followed this link to make a part of my app that involved some basic MapKit functionality. In this "part", I have to parse JSON data into "issue locations" (it's an app for a school newspaper, and we're going to have a map of all locations where they drop off issues).
Here is my problem (I don't understand it, as I am a beginner iOS developer). I get an error on the following line, that says "Variable 'jsonObject' used before being initialized".
The line in question is the third "paragraph" in the "loadInitialData" function.
if let jsonObject = jsonObject as? [String: AnyObject],
And here is all of my code for the "MapViewController.swift" file governing everything that happens in my MapKit view.
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
// Map View outlet declaration
@IBOutlet weak var mapView: MKMapView!
// Hamburger button declaration
@IBOutlet weak var menuButton:UIBarButtonItem!
var locationManager: CLLocationManager?
// Creating a variable to hold the "IssueLocation" objects from the JSON file
var issueLocations = [IssueLocation]()
override func viewDidLoad() {
super.viewDidLoad()
// Hamburger button configuration
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
menuButton.action = "revealToggle:"
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
// Setting the Map type to standard
mapView.mapType = MKMapType.Standard
// Configuring locationManager and mapView delegate
locationManager = CLLocationManager()
mapView.delegate = self
// Set the center of campus as the first location, before we show the actual user location
let initialLocation = CLLocation(latitude: 44.226397, longitude: -76.495571)
let regionRadius: CLLocationDistance = 1000
// Centering map on center of campus
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)
}
loadInitialData()
mapView.addAnnotations(issueLocations)
// Show user location and start updating user location
mapView.showsUserLocation = true
locationManager?.startUpdatingLocation()
// // Show a sample issue location on the Map
// let IssueLocation = IssueLocation(locationName: "Stirling Hall, West Entrance", coordinate: CLLocationCoordinate2D(latitude: 44.22468034747186, longitude: -76.49805217981339))
//
// mapView.addAnnotation(IssueLocation)
// Do any additional setup after loading the view.
}
func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
mapView.centerCoordinate = userLocation.location!.coordinate
}
func loadInitialData() {
let fileName = NSBundle.mainBundle().pathForResource("IssueLocations", ofType: "json");
var readError : NSError?
var data: NSData?
do {
data = try NSData(contentsOfFile: fileName!, options: NSDataReadingOptions(rawValue: 0))
} catch _ {
data = nil
}
var error: NSError?
let jsonObject: AnyObject!
if let data = data {
do {
jsonObject = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(rawValue: 0))
} catch _ {
jsonObject = nil
}
}
if let jsonObject = jsonObject as? [String: AnyObject],
let jsonData = JSONValue.fromObject(jsonObject)?["data"]?.array {
for issueLocationJSON in jsonData {
let issueLocation = IssueLocation.fromJSON(issueLocationJSON.array!)
issueLocations.append(issueLocation)
}
}
}
// Checking location authorization status and requesting permission from user if status is not ".AuthorizedWhenInUse"
func checkLocationAuthorizationStatus() {
if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse {
mapView.showsUserLocation = true
} else {
locationManager?.requestWhenInUseAuthorization()
}
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
checkLocationAuthorizationStatus()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Upvotes: 0
Views: 702
Reputation: 551
If if let data = data
evaluates to false
, then jsonObject
will not be initialized since all the initialization for that object happens inside that if statement. Try initializing jsonObject
to nil
when you create the variable.
Upvotes: 2