【问题标题】:App Crashes when after Initiating IOS Map GPS navigation and returning to the app启动 IOS 地图 GPS 导航并返回应用程序后应用程序崩溃
【发布时间】:2016-04-15 20:50:34
【问题描述】:

我正在尝试使用以下代码启动 GPS 导航,但我的应用程序崩溃并且它没有告诉我任何信息。地图应用程序和轮流导航启动正常,启动后运行良好,但我的应用程序崩溃。

更新

对不起,让我添加更多上下文。在导航和 GPS 启动并尝试返回我的应用程序后,应用程序崩溃。这是完整的代码。

 import UIKit
import MapKit
import CoreData

class DetailViewController: UIViewController  {

    var pho : Pho? = nil

    @IBOutlet var titleLabel: UILabel!

    @IBOutlet var phoneLabel: UILabel!

    @IBOutlet var addressLabel: UILabel!

    @IBOutlet var zomatoLabel: UILabel!

    @IBOutlet var googleLabel: UILabel!

    @IBOutlet var yelpLabel: UILabel!

    @IBOutlet var mapView: MKMapView!

    @IBOutlet var saveButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.



        // We need just to get the documents folder url
        let documentsUrl =  NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!

        //         now lets get the directory contents (including folders)

//        do {
//            let directoryContents = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(documentsUrl, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions())
//            print(directoryContents)
//            
//            for x in directoryContents {
//                print(x)
//            }
//            
//        } catch let error as NSError {
//            print(error.localizedDescription)
//        }




    }

    override func viewWillAppear(animated: Bool) {

        self.titleLabel.text = self.pho!.name
        self.phoneLabel.text = String(self.pho!.phoneNumber)
        self.addressLabel.text = self.pho!.address

        self.zomatoLabel.text = "\(self.pho!.rating) - \(self.pho!.votes) reviews"

        self.googleLabel.text = "\(self.pho!.gRating)"

        self.yelpLabel.text = "\(self.pho!.yRating) - \(self.pho!.yVotes) reviews"

        let latitudeAnn:CLLocationDegrees = self.pho!.latitude
        let longitudeAnn:CLLocationDegrees = self.pho!.longitude

        let latDelta:CLLocationDegrees = 0.05
        let lonDelta:CLLocationDegrees = 0.05

        let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)

        let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitudeAnn, longitudeAnn)

        let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)

        let annotation = MKPointAnnotation()

        annotation.coordinate = location

        self.mapView.addAnnotation(annotation)

        self.mapView.setRegion(region, animated: true)
        let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

        let context: NSManagedObjectContext = appDel.managedObjectContext

        let request = NSFetchRequest(entityName: "Restaurant")
        //        request.predicate = NSPredicate(format: "latitude = %@", latitude)
        let firstPredicate = NSPredicate(format: "name = %@", "\(self.pho!.name)")
        let secondPredicate = NSPredicate(format: "address = %@", "\(self.pho!.address)")
        request.returnsObjectsAsFaults = false

        request.predicate = NSCompoundPredicate(type: NSCompoundPredicateType.AndPredicateType, subpredicates: [firstPredicate, secondPredicate])

        do {

            let results = try context.executeFetchRequest(request)

            if results.count > 0 {
                print(results)
                self.saveButton.hidden = true

                for result in results as! [NSManagedObject] {


                }
            }
        } catch {
        }
    }


    @IBAction func initDirection(sender: AnyObject) {



                let latitudeAnn:CLLocationDegrees = self.pho!.latitude
                let longitudeAnn:CLLocationDegrees = self.pho!.longitude

                let coordinates:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitudeAnn, longitudeAnn)

                let launchOptions = [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]
                let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
                let mapItem = MKMapItem(placemark: placemark)

                mapItem.name = "\(self.pho!.name)"

                mapItem.openInMapsWithLaunchOptions(launchOptions)


    }

    @IBAction func savePho(sender: AnyObject) {

        self.saveButton.hidden = true

        let appDel: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

        let context: NSManagedObjectContext = appDel.managedObjectContext

        let newRestaurant = NSEntityDescription.insertNewObjectForEntityForName("Restaurant", inManagedObjectContext: context)

        newRestaurant.setValue(self.pho!.latitude, forKey: "latitude")

        newRestaurant.setValue(self.pho!.longitude, forKey: "longitude")

        newRestaurant.setValue(self.pho!.name, forKey: "name")

        newRestaurant.setValue(self.pho!.phoneNumber, forKey: "phone")

        newRestaurant.setValue(self.pho!.address, forKey: "address")

        do {
            try context.save()
        } catch {
            print("There was a problem")
        }
    }

}

【问题讨论】:

  • 崩溃的细节是什么?
  • 没说。这就是让我感到沮丧的地方。

标签: ios swift dictionary gps mapkit


【解决方案1】:

所以我发现 XML 已损坏。我的代码很好。我创建了另一个视图控制器,添加了所有小部件并连接了我的代码,一切正常。

【讨论】:

    猜你喜欢
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 2018-11-27
    • 1970-01-01
    相关资源
    最近更新 更多