[英]Parsing JSON - "Variable 'jsonObject' used before being initialized
我在運行於OS X El Cap的Xcode 7 beta 5上的iOS 9.0項目中使用的JSON庫存在問題。
我點擊了此鏈接 ,使我的應用程序包含了一些基本的MapKit功能。 在此“部分”中,我必須將JSON數據解析為“問題位置”(這是一份學校報紙的應用程序,我們將獲得所有問題發生地點的地圖)。
這是我的問題(因為我是iOS初學者,所以我不理解)。 我在下一行收到錯誤消息,提示“初始化前使用了變量'jsonObject'”。
該行是“ loadInitialData”函數中的第三個“段落”。
if let jsonObject = jsonObject as? [String: AnyObject],
這是“ MapViewController.swift”文件的所有代碼,用於管理MapKit視圖中發生的所有事情。
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.
}
*/
}
如果if let data = data
計算結果為false
,則不會初始化jsonObject
因為該對象的所有初始化都在if語句內進行。 創建變量時,嘗試將jsonObject
初始化為nil
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.