如何在点击 AnnotationCallout 时检索 NSDictionary 的实例? Swift

How to retrieve instance of NSDictionary when AnnotationCallout is tapped? Swift

我的应用程序使用 Yelp API 生成 json 数据,我将其存储到 NSDictionary 中。用户单击类别,然后地图视图使用来自 Yelp API 客户端的信息填充图钉。当用户点击注释标注时,我想显示一个详细视图控制器,其中包含来自 Yelp 客户端的数据字典的内容。我想要业务数组的那个实例,以便我可以在详细信息视图控制器上正确显示内容。

这是我的代码:

这是我查询业务的业务模型:

class Resturant: NSObject {
    var name: String!
    var thumbUrl: String!
    var address: String!
    var jsonData: NSData!
    var location: NSDictionary!        // location


    init(dictionary: NSDictionary) {
        name = dictionary["name"] as? String
        thumbUrl = dictionary["thumbUrl"] as? String
        address = dictionary["address"] as? String
        location = dictionary["location"] as? NSDictionary
    }

    class func searchWithQuery(map: MKMapView, query: String, completion: ([Resturant]!, NSError!) -> Void) {
        YelpClient.sharedInstance.searchWithTerm(query,sort: 0, radius: 1069, success: { (operation: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
            let responseInfo = response as! NSDictionary
            resultQueryDictionary = responseInfo
            println(responseInfo)
            let dataArray = responseInfo["businesses"] as! NSArray
            for business in dataArray {
                let obj = business as! NSDictionary
                var yelpBusinessMock: YelpBusiness = YelpBusiness(dictionary: obj)
                var annotation = MKPointAnnotation()
                annotation.coordinate = yelpBusinessMock.location.coordinate
                annotation.title = yelpBusinessMock.name
                annotation.subtitle = yelpBusinessMock.displayAddress
                map.addAnnotation(annotation)
            }
            }) { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
         println(error)
        }
    }

这是我的委托方法calloutAccessoryViewTapped

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
        if (control == view.rightCalloutAccessoryView) {
            let selectedLocation = view.annotation;
            let selectedCoordinate = view.annotation.coordinate;
            var latitude = selectedCoordinate.latitude
            var longitude = selectedCoordinate.longitude
            var location:CLLocation = CLLocation(latitude: latitude, longitude: longitude)
            let businessPlacemark = MKPlacemark(coordinate: selectedCoordinate, addressDictionary: nil)
            indicatedMapItem = selectedCoordinate;
            let resturantMock:Resturant = Resturant(dictionary: resultQueryDictionary)
            attractionDict = resturantMock.location;
            performSegueWithIdentifier("attractionToDetail", sender: self);
        }
    }

这是我的回购的 link:https://github.com/ssharif6/Park-N-Go

同样,这是我的 Yelp 数据的结构:

有一个数组包含不同的 NSDictionaries of Business。我想在点击标注附件视图时检索 Dictionary 的正确实例。

谢谢!

MKAnnotation 是一个协议。您可以创建任何符合该协议的自定义 object。我建议放弃 MKPointAnnotation object 并创建一个符合 MKAnnotation 协议的自定义 object ,并存储您的信息字典或具有您的所有值的属性需要。然后,当用户点击注释视图时,注释 object 包含您需要的所有信息。

或者,您可以创建 MKPointAnnotation 的自定义子 class,其中包含数据数组中的整数索引。

编辑:

在您的代码中,您具有以下内容:

var annotation = MKPointAnnotation()

创建一个符合 MKAnnoation 协议的自定义 class 而不是那样做:

class MyAnnotation: MKAnnotation
{
  //Define the coordinate title (and optionally subtitle) 
  //properties from MKAnnotation protocol

  var annotationDictionary: Dictionary
}

然后

var annotation = MyAnnotation()

像您一样设置坐标、标题和副标题,并用您需要的数据设置字典属性。然后像现在一样将 MyAnnotation object 添加到地图中。