gpt4 book ai didi

xcode - 场景类的更改导致错误,Xcode/Swift

转载 作者:行者123 更新时间:2023-11-30 13:38:36 24 4
gpt4 key购买 nike

我正在尝试制作一个小型 iPad 应用程序,其中包含“关于”页面。当我尝试更改以模式显示的关于页面场景的类时,我遇到了问题,因为当用户尝试访问此页面时应用程序崩溃。我希望将其类更改为“ViewController”,它与我的主窗口( map View )是同一类,以便通过“关于”页面上的按钮创建操作。

目前,我无法从“关于”页面按住 Ctrl 键并拖动到 .Swift 文件,除非更改其类。不知道为什么会发生这种情况:(

控制台打印的错误如下:

fatal error: unexpectedly found nil while unwrapping an Optional value
0 specialised_fatalErrorMessage(StaticString, StaticString, StaticString, Uint) -> () –

这是我的场景 Storyboard:

enter image description here

这是完整的 ViewController.Swift 代码:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UISearchBarDelegate, UIPopoverPresentationControllerDelegate {

var location: CLLocation!
let locationManager = CLLocationManager()

// Map variables
var searchController:UISearchController!
var annotation:MKAnnotation!
var localSearchRequest:MKLocalSearchRequest!
var localSearch:MKLocalSearch!
var localSearchResponse:MKLocalSearchResponse!
var error:NSError!
var pointAnnotation:MKPointAnnotation!
var pinAnnotationView:MKPinAnnotationView!

@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var segmentedControl: UISegmentedControl!
@IBOutlet weak var showSearchBar: UIBarButtonItem!
@IBOutlet weak var addButton: UIBarButtonItem!
@IBOutlet weak var moreStuff: UIBarButtonItem!

@IBAction func addButton(sender: AnyObject) {
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: self.mapView.userLocation.coordinate.latitude, longitude: self.mapView.userLocation.coordinate.longitude)
self.mapView.addAnnotation(annotation)
self.locationManager.startUpdatingLocation()

}

@IBAction func showSearchBar(sender: UIBarButtonItem!) {
searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
self.searchController.searchBar.delegate = self
presentViewController(searchController, animated: true, completion: nil)

}

@IBAction func moreStuff(sender: AnyObject) {
self.performSegueWithIdentifier("showMoreStuff", sender:self)
}


@IBAction func refresh(sender: AnyObject) {

self.locationManager.startUpdatingLocation()
}

@IBAction func segmentedControl(sender: UISegmentedControl!) {

if sender.selectedSegmentIndex == 0{

mapView.mapType = MKMapType.Standard
}
else if sender.selectedSegmentIndex == 1{

mapView.mapType = MKMapType.Satellite
}
else if sender.selectedSegmentIndex == 2{

mapView.mapType = MKMapType.Hybrid
}
}

override func viewDidLoad() {
super.viewDidLoad()

self.locationManager.delegate = self

self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

self.locationManager.requestWhenInUseAuthorization()

self.locationManager.startUpdatingLocation()

self.mapView.showsUserLocation = true

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// location delegate methods

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let location = locations.last

let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

self.mapView.setRegion(region, animated: true)

self.locationManager.stopUpdatingLocation()

}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Error code: " + error.localizedDescription)
}

func searchBarSearchButtonClicked(searchBar: UISearchBar){

searchBar.resignFirstResponder()
dismissViewControllerAnimated(true, completion: nil)
if self.mapView.annotations.count != 0{
annotation = self.mapView.annotations[0]
self.mapView.removeAnnotation(annotation)
}

localSearchRequest = MKLocalSearchRequest()
localSearchRequest.naturalLanguageQuery = searchBar.text
localSearch = MKLocalSearch(request: localSearchRequest)
localSearch.startWithCompletionHandler { (localSearchResponse, error) -> Void in

if localSearchResponse == nil{
let alertController = UIAlertController(title: nil, message: "No Such Place", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
return
}

self.pointAnnotation = MKPointAnnotation()
self.pointAnnotation.title = searchBar.text
self.pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: localSearchResponse!.boundingRegion.center.latitude, longitude: localSearchResponse!.boundingRegion.center.longitude)


self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil)
self.mapView.centerCoordinate = self.pointAnnotation.coordinate
}
}

}

我是 Swift 新手,因此非常感谢任何帮助。谢谢!

编辑:这是应用程序崩溃后呈现给我的错误屏幕: enter image description here

最佳答案

我没有使用过mapKit,但我仍在尝试。

查看您的错误,您遇到了零崩溃,这表明您的某个属性为零,但您尝试使用它。

快速浏览一下你的代码,发现你还没有初始化

var localSearchResponse....

再说一次,我不太了解 MK API,所以我不能 100 确定我所说的是否相关。

您是否检查过所有 IBOutlet 均已正确连接?

发生这种情况时,您在哪一行发生崩溃?您是否在切换到“关于”页面之前或之后遇到崩溃?

关于xcode - 场景类的更改导致错误,Xcode/Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35826808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com