我在Xcode 7 beta 5上使用的JSON库出现问题,在OS X El Cap上运行。
我跟着this link制作了我的应用程序的一部分,其中涉及一些基本的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.
}
*/
}
答案 0 :(得分:1)
如果if let data = data
的计算结果为false
,那么jsonObject
将不会被初始化,因为该对象的所有初始化都发生在if语句中。创建变量时,请尝试将jsonObject
初始化为nil
。