EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)与dataTaskWithUrl [英] EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) with dataTaskWithUrl
问题描述
我正在使用Google Places API搜索附近的地方。但是,我只想要特定类型的地方。当我仅指定一种类型时,代码(如下所示)有效,但是当我添加第二种代码时,我的代码运行并迅速在此行上给我一个EXC_BAD_INSTRUCTION(code = EXC_I386_INVOP,subcode = 0x0)错误:
I'm using the google places api to search for nearby places. However, I only want places of specific types. The code (seen below) works when I specify just one type, but when I add a second my code runs and promptly give me a EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) error on this line:
session.dataTaskWithURL(url!,complementHandler:{(数据:NSData !,响应:NSURLResponse !,错误:NSError!)->无效
我知道网址是有效的,可以将其插入浏览器并查看json,所以我不明白问题出在哪里。
I know the url is valid. I can plug it into the browser and see the json, so I don't understand what the problem is.
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
}
推荐答案
我知道url有效
I know the url is valid
URL 无效有效。您不不知道您认为自己知道什么。监听运行时。它比您了解的更多。
The URL is not valid. You do not know what you think you know. Listen to the runtime. It knows more than you do.
只需单独尝试以下代码(例如,在操场上):
Just try this code alone (in a playground, for instance):
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
is nil. And that's your problem. You cannot force-unwrap nil
; you will crash if you do.
一旦您确认了这一点,便可以开始考虑为什么该URL无效。 (很明显为什么会这样。)学会相信编译器和运行时是成功编程的关键。
Once you acknowledge this, you can start to think about why the URL is not valid. (It's pretty obvious why that might be.) Learning to believe the compiler and the runtime is key to successful programming.
提示:这样形成您的网址,一切都很好:
HINT: Form your URL like this and all is well:
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")
为什么会这样呢?看一下文档,看看这个初始化程序能为您做什么...
Why do you suppose that is? Look at the docs and see what this initializer does for you...
这篇关于EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)与dataTaskWithUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!