2016-08-05 9 views
1

Google APIを使用してルートデータを取得しようとしていますが、クラッシュしています。それは私がスウィフトGoogle Directions API JSON - EXC_BAD_INSTRUCTION(コード= EXC_I386_INVOP、サブコード= 0x0)

if let dictionary: Dictionary<NSObject, AnyObject> = try NSJSONSerialization.JSONObjectWithData(directionsData!, options: .MutableContainers) as? Dictionary<NSObject, AnyObject> { 
内のコードをすべて削除した場合でも

self.overviewPolyline = self.selectedRoute["overview_polyline"] as! Dictionary<NSObject, AnyObject> 

との "EXC_BAD_INSTRUCTION(コード= EXC_I386_INVOP、サブコード= 0x0のを)" と言う

let baseURLDirections = "https://maps.googleapis.com/maps/api/directions/json?" 
var selectedRoute: Dictionary<NSObject, AnyObject>! 
var overviewPolyline: Dictionary<NSObject, AnyObject>! 
var originCoordinate: CLLocationCoordinate2D! 
var destinationCoordinate: CLLocationCoordinate2D! 
var originAddress: String! 
var destinationAddress: String! 

func getDirections(origin: String, destination: String, waypoints: Array<String>!, travelMode: AnyObject!, completionHandler: ((status: String, success: Bool) -> Void)) { 

    var directionsURLString = baseURLDirections + "origin=" + origin + "&destination=" + destination 

    directionsURLString = directionsURLString.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())! 

    let directionsURL = NSURL(string: directionsURLString) 
    let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: directionsURL!) 
    let session = NSURLSession.sharedSession() 
    let task = session.dataTaskWithRequest(urlRequest) { 
     (data, response, error) -> Void in 

     do { 
      let directionsData = NSData(contentsOfURL: directionsURL!) 
      if let dictionary: Dictionary<NSObject, AnyObject> = try NSJSONSerialization.JSONObjectWithData(directionsData!, options: .MutableContainers) as? Dictionary<NSObject, AnyObject> { 

       let status = dictionary["status"] as! String 

       if status == "OK" { 
        self.selectedRoute = (dictionary["routes"] as! Array<Dictionary<NSObject, AnyObject>>)[0] 

        self.overviewPolyline = self.selectedRoute["overview_polyline"] as! Dictionary<NSObject, AnyObject> 

        let legs = self.selectedRoute["legs"] as! Array<Dictionary<NSObject, AnyObject>> 

        let startLocationDictionary = legs[0]["start_location"] as! Dictionary<NSObject, AnyObject> 
        self.originCoordinate = CLLocationCoordinate2DMake(startLocationDictionary["lat"] as! Double, startLocationDictionary["lng"] as! Double) 

        let endLocationDictionary = legs[legs.count - 1]["end_location"]as! Dictionary<NSObject, AnyObject> 
        self.destinationCoordinate = CLLocationCoordinate2DMake(endLocationDictionary["lat"] as! Double, endLocationDictionary["lng"] as! Double) 

        self.originAddress = legs[0]["start_address"]as! String 
        self.destinationAddress = legs[legs.count - 1]["end_address"]as! String 

        self.calculateTotalDistanceAndDuration() 

        completionHandler(status: status, success: true) 
       } 
       else { 
        completionHandler(status: status, success: false) 
       } 
      } 
     } catch let error as NSError { 
      print(error) 
      completionHandler(status: "", success: false) 
     } 
    } 

    task.resume() 
} 

:ここに私のコードです

私はEXC_BAD_INSTRUCTION(コード= EXC_I386_INVOP、サブコード= 0x0)を取得しています。 これをどのように解決すればよいですか?

+0

はそのoptionalsを確認してくださいあなたは力のアンラッピングは実際にはゼロではありません。 –

+0

データを2回ダウンロードしています! NSURLSessionを使用して1回、NSDataを使用して1回 – Moritz

答えて

0

それは私が

directionsURLString = directionsURLString.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())! 
0

directionsURLString = directionsURLString.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())! 

を変更したときに、私は

directionsURLString = directionsURLString.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())! 
0123に

directionsURLString = directionsURLString.stringByAddingPercentEncodingWithAllowedCharacters(.UR>LHostAllowedCharacterSet())! 

を変更したときにそれが働いて働いていました

私は正しい方向に向いてくれてありがとう! SWIFT 3.0では それはに変換:ここ

SearchString = SearchString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)! 

(アクセントと「O」のようなそうではなく、奇妙な文字を削除します)URLの準備のための別の有用な文字列の修飾子です:

SearchString = SearchString.folding(options: .diacriticInsensitive, locale: .current) 
関連する問題