现在我有一个mapview,根据一个类别填充一堆引脚,例如,“亚洲食品”#。当我点击该注释的标注附件时,应用程序导航到详细视图控制器,我希望显示地址和其他属性。我从Yelp API CLient获取信息。这是我的模特:
import UIKit
import MapKit
var resultQueryDictionary:NSDictionary!
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
self.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
attractionDetailAddressString = yelpBusinessMock.displayAddress
map.addAnnotation(annotation)
}
}) { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
println(error)
}
}
// term: String, deal: Bool, radius: Int, sort: Int, categories: String, success: (AFHTTPRequestOperation!, AnyObject!) -> Void, failure: (AFHTTPRequestOperation!, NSError!) -> Void) -> AFHTTPRequestOperation! {
class func searchWithQueryWithRadius(map: MKMapView, term: String, deal: Bool, radius: Int, sort: Int, categories: String, completion: ([Resturant]!, NSError!) -> Void) {
YelpClient.sharedInstance.searchWithTerm(term, deal: false, radius: radius, sort: sort,categories: categories, 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
map.addAnnotation(annotation)
}
}) { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
println(error)
}
}
}
在我的景点ViewController中(用户点击类别用引脚填充地图),我有这个代码用于点击标注附件:
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);
}
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var attractionsDetailViewController:AttractionsDetailViewController = segue.destinationViewController as! AttractionsDetailViewController
attractionsDetailViewController.attractionLocation = indicatedMapItem;
attractionsDetailViewController.attractionLocationDetail = self.attractionDict
}
在我的详细信息视图控制器中,我尝试获取所点击的特定业务的数据。
func getYelpData() {
var businessMock:Resturant = Resturant(dictionary: resultQueryDictionary)
var address:NSDictionary = attractionLocationDetail //dictionary
attractionDetailAddressString = businessMock.location["display_address"] as? String;
self.addressLabel.text = attractionDetailAddressString
}
但是,这显示地址为零。
基本上我想知道如何从大型企业的NSDictionary获取业务的一个实例,然后在我的详细VC中检索该个别业务的数据。
如果您想查看所有代码,这是我的Github回购: https://github.com/ssharif6/Park-N-Go
感谢您的帮助!
答案 0 :(得分:0)
看起来你的问题可能就在这一行
attractionDict = resturantMock.location;
您的词典将有一个位置键,但不拥有该位置子词典中建议的键:
init(dictionary: NSDictionary) {
name = dictionary["name"] as? String
thumbUrl = dictionary["thumbUrl"] as? String
address = dictionary["address"] as? String
self.location = dictionary["location"] as? NSDictionary ?? [:]
}
当VC加载时,所有这些值自然会nil
。
然而,这些键确实存在于resturantMock.business
字典中 - 这是你打算写的吗? :)