如何解析NSDictionary数据

时间:2015-08-13 07:27:22

标签: ios swift nsdictionary yelp

我的应用程序当前正在使用Yelp Service API并返回yelp客户端提供的关联数据的键值对。 我的情况是我有一个景点视图控制器,可以删除不同类别的针注释,即“亚洲食品”等。当您点击针注释并点击标注时,它会带您进入详细视图控制器。

这是我这样做的地方:

git remote set-url origin https://username@github.com/org/project.git

这是我的API服务电话:

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;
            performSegueWithIdentifier("attractionToDetail", sender: self);
        }
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        var attractionsDetailViewController:AttractionsDetailViewController = segue.destinationViewController as! AttractionsDetailViewController
        attractionsDetailViewController.attractionLocation = indicatedMapItem;
    }

当我在西雅图地区查询亚洲食品时,这会打印到这样的日志数据:

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

我想要做的只是提取yelp响应数据并将我在DetailViewController中的AddressLabel文本设置为Yelp数据中的地址(因为reverseGeoCoding有小错误)。

目前我正在使用我创建的YelpBusiness.swift,但我不确定它是否正确...

{
businesses =     (
            {
        categories =             (
                            (
                Korean,
                korean
            ),
                            (
                Buffets,
                buffets
            )
        );
        "display_phone" = "+1-206-854-3166";
        distance = "124.9692597772704";
        id = "ummas-lunch-box-seattle";
        "image_url" = "http://s3-media2.fl.yelpcdn.com/bphoto/QqrpImnPywZ9zc721RVRrg/ms.jpg";
        "is_claimed" = 1;
        "is_closed" = 0;
        location =             {
            address =                 (
                "1301 5th Ave",
                Concourse
            );
            city = Seattle;
            coordinate =                 {
                latitude = "47.6088937";
                longitude = "-122.3342613";
            };
            "country_code" = US;
            "cross_streets" = "Union St & University St";
            "display_address" =                 (
                "1301 5th Ave",
                Concourse,
                Downtown,
                "Seattle, WA 98101"
            );
            "geo_accuracy" = "9.5";
            neighborhoods =                 (
                Downtown
            );
            "postal_code" = 98101;
            "state_code" = WA;
        };
        "mobile_url" = "http://m.yelp.com/biz/ummas-lunch-box-seattle";
        name = "Umma's Lunch Box";
        phone = 2068543166;
        rating = "4.5";
        "rating_img_url" = "http://s3-media2.fl.yelpcdn.com/assets/2/www/img/99493c12711e/ico/stars/v1/stars_4_half.png";
        "rating_img_url_large" = "http://s3-media4.fl.yelpcdn.com/assets/2/www/img/9f83790ff7f6/ico/stars/v1/stars_large_4_half.png";
        "rating_img_url_small" = "http://s3-media2.fl.yelpcdn.com/assets/2/www/img/a5221e66bc70/ico/stars/v1/stars_small_4_half.png";
        "review_count" = 155;
        "snippet_image_url" = "http://s3-media1.fl.yelpcdn.com/photo/H1UEaGpAztTzFIDKbsx_UA/ms.jpg";
        "snippet_text" = "Korean comfort food at an affordable price. My only wish is that they are opened longer..\n\nI'm sure that everybody here has already written about it, but it...";
        url = "http://www.yelp.com/biz/ummas-lunch-box-seattle";
    }

谢谢!

1 个答案:

答案 0 :(得分:0)

新答案

因为你正在使用NSDictionary& NSArray对象,您可以使用神奇的valueForKeyPath:

// I HIGHLY recommend a more descriptive variable name instead of "obj"
let obj = business as! NSDictionary 
let displayAddressArray = obj.valueForKeyPath("location.display_address") as NSArray

if displayAddressArray.count > 0
{
    annotation.title = "\(displayAddressArray[0])\n\(displayAddressArray[1])\n"
}

您可能可以重新格式化注释的外观,具体取决于您希望它看起来像什么。

可以找到有关此内容以及本机swift嵌套字典访问的更多信息in this helpful article I was just looking at

原始答案

如果我正确理解了您的问题,并假设YelpBusiness对象与I see in this github repo匹配,那么您需要做的事情就像更改此行一样简单:

annotation.title = yelpBusinessMock.name

为:

annotation.title = yelpBusinessMock.location.display_address