在swift中访问闭包之外的JSON数据

时间:2018-05-10 19:44:23

标签: ios json swift closures

我想知道这是否可行 - 在这个类中,即时获取JSON响应,response.businesses允许我访问闭包内的响应。然而,我的主要目的是使用此响应并将其呈现在表视图中。但是当我尝试再次调用它时,我无法访问数据,即使在声明具有相同名称的类var之后也是如此。我知道这与主线程有关,但是我想知道是否有任何有效的编码器知道任何变通方法,我在这方面相当新。谢谢

class YelpTableView: UIViewController, UITableViewDataSource, UITableViewDelegate{




    var businesses = [CDYelpBusiness]()
    var names : String?



    override func viewDidLoad() {
        super.viewDidLoad()





        let yelpAPIClient = CDYelpAPIClient(apiKey: "MY-API-KEY")
        yelpAPIClient.cancelAllPendingAPIRequests()
        CDYelpFusionKitManager.shared.apiClient = yelpAPIClient

        CDYelpFusionKitManager.shared.apiClient.searchBusinesses(byTerm: "Bars", location: "Hollywood, California", latitude: nil, longitude: nil, radius: 10000, categories: [CDYelpBusinessCategoryFilter.bars], locale: CDYelpLocale.english_unitedStates, limit: 5, offset: 0, sortBy: CDYelpBusinessSortType.distance, priceTiers: [CDYelpPriceTier.twoDollarSigns], openNow: true, openAt: nil, attributes: nil) { (response) in

            if let response = response,
                let businesses = response.businesses,
                businesses.count > 0 {

                for business in (response.businesses)! {

                    self.names = business.name
                    print(self.names)

                  //  Prints
//                    Optional("Ever Bar")
//                    Optional("Melrose Umbrella")
//                    Optional("The Pikey")
//                    Optional("Velvet Margarita")
//                    Optional("Mama Shelter Rooftop Bar")
//                    Optional("Mama Shelter Rooftop Bar")


                }
            }

            print(self.names) // prints nil
            print(self.businesses) // print [ ]
        }

    }

1 个答案:

答案 0 :(得分:1)

您需要将语言环境businesses变量分配给您的类属性。

CDYelpFusionKitManager.shared.apiClient.searchBusinesses(byTerm: "Bars", location: "Hollywood, California", latitude: nil, longitude: nil, radius: 10000, categories: [CDYelpBusinessCategoryFilter.bars], locale: CDYelpLocale.english_unitedStates, limit: 5, offset: 0, sortBy: CDYelpBusinessSortType.distance, priceTiers: [CDYelpPriceTier.twoDollarSigns], openNow: true, openAt: nil, attributes: nil) { (response) in
    if let response = response,
        let businesses = response.businesses,
        businesses.count > 0 {

        for business in (response.businesses)! {
            self.names = business.name
            print(self.names)
        }

        self.businesses = businesses
    }
}

以上内容将正确更新您的财产。在此之后,您需要告诉表视图重新加载。确保在主队列上完成重新加载。

这使您的最终代码类似于:

CDYelpFusionKitManager.shared.apiClient.searchBusinesses(byTerm: "Bars", location: "Hollywood, California", latitude: nil, longitude: nil, radius: 10000, categories: [CDYelpBusinessCategoryFilter.bars], locale: CDYelpLocale.english_unitedStates, limit: 5, offset: 0, sortBy: CDYelpBusinessSortType.distance, priceTiers: [CDYelpPriceTier.twoDollarSigns], openNow: true, openAt: nil, attributes: nil) { (response) in
    if let response = response,
        let businesses = response.businesses,
        businesses.count > 0 {

        for business in (response.businesses)! {
            self.names = business.name
            print(self.names)
        }

        self.businesses = businesses

        DispatchQueue.main.async {
            self.tablView.reloadData()
        }
    }
}