0

我正在使用MapBoxNavigation.swift以構建將用戶導航到某個位置的應用程序。直到導航開始,一切正常。但是,當導航開始,MapBoxNavigation.swift發出的帖子一NSNotification(見庫如何工作文檔),我寫的觀察員拋出此錯誤消息:無法識別的選擇器,即使選擇器名稱正確

app[49184:2686603] -[__NSCFConstantString navProgressDidChange:]: unrecognized selector sent to instance 0x1096dfdb0 

這是代碼,文章MapBoxNavigation通知。 SWIFT:

NotificationCenter.default.post(name: RouteControllerAlertLevelDidChange, object: self, userInfo: [ 
     RouteControllerAlertLevelDidChangeNotificationRouteProgressKey: routeProgress, 
     RouteControllerAlertLevelDidChangeNotificationDistanceToEndOfManeuverKey: userDistance, 
     RouteControllerProgressDidChangeNotificationIsFirstAlertForStepKey: isFirstAlertForStep 
     ]) 

這應該是所有必要的代碼:

import Foundation 
import UIKit 
import Mapbox 
import MapboxDirections 
import MapboxNavigation 
import CoreLocation 
import Alamofire 
import SWXMLHash 
import DateTimePicker 

//Primary ViewController, used for basically everything 
class ViewController: UIViewController, MGLMapViewDelegate, CLLocationManagerDelegate, UITextFieldDelegate, UIGestureRecognizerDelegate { 

    lazy var geocoder = CLGeocoder() 
    @IBOutlet weak var addressTextField: SearchTextField! //SearchTextField 
    @IBOutlet var mapView: MGLMapView! //MapView of MapBox  

    @IBOutlet weak var BookingUIView: UIView! 
    @IBOutlet weak var BookingView: UIView! 

    //Used to geocode the Addresses 
    let locationManager = CLLocationManager() 
    var searched = false 
    var keyboardEnabled = false 
    var navigationStarted = false 


    //Getting the route object 
    let directions = Directions.shared 
    let waitForInterval: TimeInterval = 5 

    @IBOutlet weak var arrivalTimeLabel: UILabel! 
    @IBOutlet weak var arrivalTimeButton: UIButton! 
    @IBOutlet weak var leaveTimeButton: UIButton! 
    @IBOutlet weak var searchAddressButton: UIButton! 


    /* Overriding Super-Functions */ 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     // This is needed.... i don't know why.. 
     mapView.delegate = self 

     //Delegate textField to Self 
     addressTextField.delegate = self 

     //Dismiss Keyboard on view Touch 
     //Looks for single or multiple taps. 
     let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard)) 
     self.mapView.addGestureRecognizer(tap) 
     //Delegate GestureRecognizer to self 
     tap.delegate = self 

     //Get the users current location and zoom to it 
     //Authorization stuff #privacy 
     self.locationManager.requestWhenInUseAuthorization() 
     //initialize the updating of the location 
     if(CLLocationManager.locationServicesEnabled()){ 
      locationManager.delegate = self 
      locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation 
      locationManager.startUpdatingLocation() 
     } 

     //Adding TextViewListener 
     addressTextField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged) 

     //Registering PresentNavigationViewControllerObserver :3 
     NotificationCenter.default.addObserver(self, selector: #selector(self.enableNavigationMode), name: NSNotification.Name(rawValue: "StartNavigation"), object: nil) 
    } 
func enableNavigationMode(){ 
     //Hiding some elements here 
     startNavigation() //Gets called properly 
    } 


    func navProgressDidChange(_ userInfo: NSNotification){ 
     print("depart") 
     print(userInfo) 
    } 

    func startNavigation(){ 
     //Get the JSON File via the MapBox API 
     var lat = locationManager.location?.coordinate.latitude 
     var long = locationManager.location?.coordinate.longitude 
     var depart = CLLocation(latitude: lat!, longitude: long!) 
     var start = Waypoint(coordinate: CLLocationCoordinate2D(latitude: lat!, longitude: long!)) 
     var end = Waypoint(coordinate: CLLocationCoordinate2D(latitude: Shared.shared.selectedParkhouse.latitude, longitude: Shared.shared.selectedParkhouse.longitude)) 

     var string = "https://api.mapbox.com/directions/v5/mapbox/driving-traffic/" + String(describing: long!) + "%2C" + String(describing: lat!) + "%3B" + String(describing: Shared.shared.selectedParkhouse.longitude) + "%2C" + String(describing: Shared.shared.selectedParkhouse.latitude) + ".json?access_token=mykey;)&steps=true" 

//Alamofire request works properly 
     Alamofire.request(string).responseData { response in 
      if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) { 

       let jsoninput = self.convertToDictionary(text: utf8Text)! 
       let jsonroute = (jsoninput["routes"] as! [AnyObject]).first as! [String:Any] 

       let route = Route(json: jsonroute, waypoints: [start, end], profileIdentifier: MBDirectionsProfileIdentifierAutomobileAvoidingTraffic) 
       let routecontroller = RouteController(route: route) 

       print("regging") 
       NotificationCenter.default.addObserver(RouteControllerAlertLevelDidChange, selector: #selector(self.navProgressDidChange(_:)), name: RouteControllerAlertLevelDidChange, object: routecontroller) 
       print("resuming") 
       routecontroller.resume() 
       print("updating(?)") 
       //Everything is fine until this function gets called and 
       //MapBoxNavigation.swift posts a Notification 

       routecontroller.locationManager(routecontroller.locationManager, didUpdateLocations: [depart]) 

      } 
     } 
    } 

我用NotificationCenter幾恬這就是爲什麼我要來這裏尋求幫助。我還嘗試了navProgressDidChange(userInfo: Notification)中的不帶參數的不同功能名稱和參數到當前在我的代碼中的參數。我不知道爲什麼會發生這種情況,而且在我看來也沒有意義,因爲選擇器對我來說似乎很好。

如果您需要更多信息,只需詢問。 感謝您的每一個答案!

+0

嘗試兩者,沒有'self'或'ViewController'(ClassName)XCode報告錯誤,因爲該函數不能直接識別。同樣在它前面有'ViewController',它不起作用。 – njoye

+0

名稱是'ViewController',發送的類是'RouteController' – njoye

回答

3

你這裏語法不正確:

NotificationCenter.default.addObserver(RouteControllerAlertLevelDidChange, selector: #selector(self.navProgressDidChange(_:)), name: RouteControllerAlertLevelDidChange, object: routecontroller) 

你訂閱的字符串RouteControllerAlertLevelDidChange的通知。你打算訂閱self。你可以看到,在錯誤信息:

app[49184:2686603] -[__NSCFConstantString navProgressDidChange:]: unrecognized selector sent to instance 0x1096dfdb0 

這是抱怨,它試圖發送消息到一個常量字符串。

+0

今天我沒有足夠的休息時間,這個解決方案正在工作。謝謝! – njoye