gpt4 book ai didi

exception - EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)与 dataTaskWithUrl

转载 作者:可可西里 更新时间:2023-10-31 23:59:24 38 4
gpt4 key购买 nike

我正在使用 google places api 来搜索附近的地点。但是,我只想要特定类型的地方。当我只指定一种类型时,代码(如下所示)有效,但是当我添加第二个类型时,我的代码运行并立即在这一行给我一个 EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) 错误:

session.dataTaskWithURL(url!, completionHandler: { (data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in

我知道网址有效。我可以将它插入浏览器并看到 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 无效。你知道你认为你知道的。听听运行时。它比你知道的更多。

只需单独尝试此代码(例如,在 Playground 上):

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")

为什么会这样?查看文档,看看这个初始化程序为您做了什么......

关于exception - EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)与 dataTaskWithUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28054030/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com