简体   繁体   English

viewForAnnotation没有被调用(使用Alamofire解析JSON)

[英]viewForAnnotation not being called (using Alamofire to parse JSON)

I'm new to iOS programming. 我是iOS编程的新手。 I prefer to use Swift. 我更喜欢使用Swift。

What I'm trying to do is call a web service that returns some JSON, parse the JSON into a custom object called Entry and plot those entries on a map. 我想做的是调用一个返回一些JSON的Web服务,将JSON解析为一个名为Entry的自定义对象,然后在地图上绘制这些条目。

I am using AlamoFire to make the web service call and SwiftyJSON to parse the JSON into Entry objects like below 我正在使用AlamoFire进行Web服务调用,并使用SwiftyJSON将JSON解析为Entry对象,如下所示

    request(.GET, URLString: "https://myURLThatReturnsJson")
        .responseJSON { (_, _, json, error) in

            if let json: AnyObject = json{

                let jsonEntries = JSON(json)

                for result in jsonEntries["entries"].arrayValue {
                    let business_name = result["business_name"].stringValue
                    let latitude = result["lat"].stringValue
                    let longitude = result["lng"].stringValue
                    let category = result["category"].stringValue
                    let address = result["address"].stringValue
                    let city = result["city"].stringValue
                    let type = result["type"].stringValue
                    let location = result["location"].stringValue
                    let valid = result["valid"].stringValue
                    let comments = result["comments"].stringValue
                    let date = result["date"].stringValue
                    let username = result["username"].stringValue
                    let additional_comment_count = result["additional_comment_count"].stringValue


                    let entry : Entry = Entry(
                        business_name: business_name,
                        latitude: latitude,
                        longitude: longitude,
                        category: category,
                        address: address,
                        city: city,
                        type: type,
                        location: location,
                        valid: valid,
                        comments: comments,
                        date: date,
                        username: username,
                        additional_comment_count: additional_comment_count,
                        title: business_name,
                        subtitle: location,
                        coordinate: CLLocationCoordinate2D(latitude: (latitude as NSString).doubleValue, longitude: (longitude as NSString).doubleValue))

                    entries.append(entry)
                }

                // Now we have our array of entries. Now plot them.

                for entry in entries{

                    self.mapView.addAnnotation(entry)

                }
            }
    }

So as you can see in the code above I am also mapping the entries within the AlamoFire request (don't know if this is bad because AlamoFire is done on a separate thread I believe). 因此,如您在上面的代码中看到的那样,我还映射了AlamoFire请求中的条目(不知道这是否不好,因为AlamoFire是在我相信的单独线程上完成的)。

I also have a viewForAnnotations method that looks like below 我也有一个viewForAnnotations方法,如下所示

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
         print("viewForAnnotation called")

         return nil
    }

My viewForAnnotation method is not being called at all (break point on print line is not being hit). 我的viewForAnnotation方法根本没有被调用(未击中打印行的断点)。

Any help is appreciated! 任何帮助表示赞赏!

Please make sure to set the delegate property of mapview to self. 请确保将mapview的委托属性设置为self。

mapview.delegate = self

You can also do this by connecting the delegate outlet of mapview to ViewController using Connection Inspector (Interface Builder) 您也可以通过使用Connection Inspector(Interface Builder)将mapview的代理出口连接到ViewController来完成此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM