Google API를 사용하여 길 찾기 데이터를 가져 오려고하는데 계속 충돌이 발생합니다. 내 코드는 다음과 같습니다.
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)"이라고 표시됩니다.
self.overviewPolyline = self.selectedRoute["overview_polyline"] as! Dictionary<NSObject, AnyObject>
그리고 내부의 모든 코드를 삭제해도
if let dictionary: Dictionary<NSObject, AnyObject> = try NSJSONSerialization.JSONObjectWithData(directionsData!, options: .MutableContainers) as? Dictionary<NSObject, AnyObject> {
EXC_BAD_INSTRUCTION (코드 = EXC_I386_INVOP, 하위 코드 = 0x0)을 받고 있습니다. 이 문제를 어떻게 해결합니까?
내가 변했을 때 효과가 있었어
directionsURLString = directionsURLString.stringByAddingPercentEncodingWithAllowedCharacters(.UR>LHostAllowedCharacterSet())!
...에
directionsURLString = directionsURLString.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())!
저를 올바른 방향으로 알려 주셔서 감사합니다! 신속한 3.0에서는 다음과 같이 번역됩니다.
SearchString = SearchString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
다음은 URL 준비를위한 또 다른 유용한 문자열 수정 자입니다 (악센트 등을 제거하지만 "ø"와 같은 이상한 문자는 제거하지 않음).
SearchString = SearchString.folding(options: .diacriticInsensitive, locale: .current)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다