一尘不染

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

swift

我正在使用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
}

阅读 319

收藏
2020-07-07

共1个答案

一尘不染

我知道网址有效

网址是 不是 有效。您 知道自己以为所知道的。监听运行时。它比您了解的更多。

只需单独尝试以下代码(例如,在操场上):

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

你为什么这么认为呢?查看文档,看看此初始化程序可以为您做什么…

2020-07-07