EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)与 dataTaskWithUrl
EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) with dataTaskWithUrl
我正在使用 google 个地点 api 来搜索附近的地点。但是,我只想要特定类型的地方。当我只指定一种类型时,代码(如下所示)有效,但是当我添加第二个类型时,我的代码运行并立即在这一行给我一个 EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) 错误:
session.dataTaskWithURL(url!, completionHandler: { (data : NSData!, response : NSURLResponse!, error : NSError!) -> Void in
我知道 url 有效。我可以将它插入浏览器并看到json,所以我不明白问题是什么。
func search(location : CLLocationCoordinate2D, radius : Int, callback : (items : [Attraction]?, errorDescription : String?) -> Void) {
var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=37.7873589,-122.408227&radius=4000&types=aquarium|art_gallery&key=YOURKEY"
var url = NSURL(string: urlString)
var session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
session.dataTaskWithURL(url!, completionHandler: { (data : NSData!, response : NSURLResponse!, error : NSError!) -> Void in
if error != nil {
callback(items: nil, errorDescription: error.localizedDescription)
}
if let statusCode = response as? NSHTTPURLResponse {
if statusCode.statusCode != 200 {
callback(items: nil, errorDescription: "Could not continue. HTTP Status Code was \(statusCode)")
}
}
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
callback(items: GooglePlaces.parseFromData(data), errorDescription: nil)
})
}).resume()
}
class func parseFromData(data : NSData) -> [Attraction] {
var attractions = [Attraction]()
var json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
var results = json["results"] as? [NSDictionary]
for result in results! {
var placeId = result["place_id"] as String
var image = result["icon"] as String
var name = result["name"] as String
var ratingString = ""
var types = result["types"] as [String]
println(types)
if result["rating"] != nil {
var rating = result["rating"] as Double
ratingString = "\(rating)"
}
var coordinate : CLLocationCoordinate2D!
if let geometry = result["geometry"] as? NSDictionary {
if let location = geometry["location"] as? NSDictionary {
var lat = location["lat"] as CLLocationDegrees
var long = location["lng"] as CLLocationDegrees
coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
var placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
var attraction = Attraction(id: placeId, imageUrl: "image url", locationName: name, ratingAvg: "\(ratingString)", types: types, placemarker: placemark)
attractions.append(attraction)
}
}
}
return attractions
}
I know the url is valid
URL无效。你确实不知道你认为你知道的。听听运行时。它比你知道的更多。
只需单独尝试此代码(例如在操场上):
var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=37.7873589,-122.408227&radius=4000&types=aquarium|art_gallery&key=YOURKEY"
let url = NSURL(string:urlString)
url
为零。那是你的问题。你不能强制展开 nil
;如果你这样做,你会崩溃。
一旦你承认这一点,你就可以开始思考为什么 URL 是无效的。 (原因很明显。)学会相信编译器和运行时是成功编程的关键。
提示:像这样形成你的URL,一切都很好:
let url2 = NSURL(scheme: "https", host: "maps.googleapis.com", path: "/maps/api/place/nearbysearch/json?location=37.7873589,-122.408227&radius=4000&types=aquarium|art_gallery&key=YOURKEY")
你认为这是为什么?查看文档并了解此初始化程序为您做了什么...
我会用.stringByAddingPercentEscapesUsingEncoding()
var urlString = "http://example.com/?foo=bar|baz"
if var escapedURLString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
NSURL(string: escapedURLString)
}
Returns: http://example.com/?foo=bar%7Cbaz
我正在使用 google 个地点 api 来搜索附近的地点。但是,我只想要特定类型的地方。当我只指定一种类型时,代码(如下所示)有效,但是当我添加第二个类型时,我的代码运行并立即在这一行给我一个 EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) 错误:
session.dataTaskWithURL(url!, completionHandler: { (data : NSData!, response : NSURLResponse!, error : NSError!) -> Void in
我知道 url 有效。我可以将它插入浏览器并看到json,所以我不明白问题是什么。
func search(location : CLLocationCoordinate2D, radius : Int, callback : (items : [Attraction]?, errorDescription : String?) -> Void) {
var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=37.7873589,-122.408227&radius=4000&types=aquarium|art_gallery&key=YOURKEY"
var url = NSURL(string: urlString)
var session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
session.dataTaskWithURL(url!, completionHandler: { (data : NSData!, response : NSURLResponse!, error : NSError!) -> Void in
if error != nil {
callback(items: nil, errorDescription: error.localizedDescription)
}
if let statusCode = response as? NSHTTPURLResponse {
if statusCode.statusCode != 200 {
callback(items: nil, errorDescription: "Could not continue. HTTP Status Code was \(statusCode)")
}
}
NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
callback(items: GooglePlaces.parseFromData(data), errorDescription: nil)
})
}).resume()
}
class func parseFromData(data : NSData) -> [Attraction] {
var attractions = [Attraction]()
var json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
var results = json["results"] as? [NSDictionary]
for result in results! {
var placeId = result["place_id"] as String
var image = result["icon"] as String
var name = result["name"] as String
var ratingString = ""
var types = result["types"] as [String]
println(types)
if result["rating"] != nil {
var rating = result["rating"] as Double
ratingString = "\(rating)"
}
var coordinate : CLLocationCoordinate2D!
if let geometry = result["geometry"] as? NSDictionary {
if let location = geometry["location"] as? NSDictionary {
var lat = location["lat"] as CLLocationDegrees
var long = location["lng"] as CLLocationDegrees
coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
var placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
var attraction = Attraction(id: placeId, imageUrl: "image url", locationName: name, ratingAvg: "\(ratingString)", types: types, placemarker: placemark)
attractions.append(attraction)
}
}
}
return attractions
}
I know the url is valid
URL无效。你确实不知道你认为你知道的。听听运行时。它比你知道的更多。
只需单独尝试此代码(例如在操场上):
var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=37.7873589,-122.408227&radius=4000&types=aquarium|art_gallery&key=YOURKEY"
let url = NSURL(string:urlString)
url
为零。那是你的问题。你不能强制展开 nil
;如果你这样做,你会崩溃。
一旦你承认这一点,你就可以开始思考为什么 URL 是无效的。 (原因很明显。)学会相信编译器和运行时是成功编程的关键。
提示:像这样形成你的URL,一切都很好:
let url2 = NSURL(scheme: "https", host: "maps.googleapis.com", path: "/maps/api/place/nearbysearch/json?location=37.7873589,-122.408227&radius=4000&types=aquarium|art_gallery&key=YOURKEY")
你认为这是为什么?查看文档并了解此初始化程序为您做了什么...
我会用.stringByAddingPercentEscapesUsingEncoding()
var urlString = "http://example.com/?foo=bar|baz"
if var escapedURLString = urlString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) {
NSURL(string: escapedURLString)
}
Returns: http://example.com/?foo=bar%7Cbaz