【发布时间】:2015-11-10 18:46:04
【问题描述】:
我在用于 iOS 9.0 项目、在 Xcode 7 beta 5 上、在 OS X El Cap 上运行的 JSON 库时遇到问题。
我跟随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.
}
*/
}
【问题讨论】: