ios - How to pull information from NSDictionary Swift -
right have mapview populates bunch of pins based on category, example, 'asian food'. when click callout accessory of annotation, app navigates detail viewcontroller, hope display address , other properties. getting information yelp api client. here model:
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 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 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) } } }
in attractions viewcontroller (where user taps categories populate map pins), have code when callout accessory tapped:
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 }
in detail view controller, attempt data specific business tapped.
func getyelpdata() { var businessmock:resturant = resturant(dictionary: resultquerydictionary) var address:nsdictionary = attractionlocationdetail //dictionary attractiondetailaddressstring = businessmock.location["display_address"] as? string; self.addresslabel.text = attractiondetailaddressstring }
however, displays address nil.
basically want know how 1 instance of business large nsdictionary of businesses , retrieve data individual business in detail vc.
this github repo if want @ code: https://github.com/ssharif6/park-n-go
thanks help!
it looks though problem may here on line
attractiondict = resturantmock.location;
your dictionary have location key, doesn't have keys suggested here on location sub dictionary:
init(dictionary: nsdictionary) { name = dictionary["name"] as? string thumburl = dictionary["thumburl"] as? string address = dictionary["address"] as? string self.location = dictionary["location"] as? nsdictionary ?? [:] }
so naturally values nil
when vc gets loaded. keys exist on resturantmock.business
dictionary - meant write? :)
Comments
Post a Comment