首页 > 解决方案 > 为什么 YELP API 中的文本不显示在我的标签上?

问题描述

基本上我使用YELP API接收特定餐厅的评论,我想将评论显示到我的SKLabel. 我可以在控制台中看到打印出来的评论,但是当我尝试text从我的结构中获取值时,它不会出现在SKLabel. 这是我正在使用的代码。谢谢!

extension GameScene {

func fetchReviews(id: String, //Required
                  locale: String, //Optional
                  completionHandler: @escaping ([Reviews]?, Error) -> Void) {
    
    
    // MARK: Retrieve venues from Yelp API
    let apikey = "API KEY"
    
    /// create URL
    let baseURL = "https://api.yelp.com/v3/businesses/\(id)/reviews"
    

    let url = URL(string: baseURL)
    
    print("this is the url for reviews : \(url)")
    
    /// Creating request
    var request = URLRequest(url: url!)
    request.setValue("Bearer \(apikey)", forHTTPHeaderField: "Authorization")
    request.httpMethod = "GET"
    
    
    URLSession.shared.dataTask(with: request) { (data, response, error) in
        if let err = error {
            print(err.localizedDescription)
        }
        do {
            let json = try JSONSerialization.jsonObject(with: data!, options: []) as! [String: Any]
            print(">>>>>", json, #line, "<<<<<<<<<")
        } catch {
            print("caught")
        }
    }.resume()
   }
  }


  //Reviews.swift
  
 struct Reviews {
  var text : String?
  var locale : String?
  var id : String?
  var total : Int?
  var rating: Int?
}  


//GameScene.swift

fetchReviews(id: venue.id!, locale: "en_US") { (response, error) in
              
                 for review in response! {

                
                 reviewLabel.text = review.text //Doesn't work here
                 reviewLabel.position = CGPoint(self.size.width / 2, self.size.height / 2)
                 reviewLabel.fontColor = .white
                 reviewLabel.fontSize = 20
                 addChild(reviewLabel)
              }
           }

标签: swiftxcodeyelp-fusion-api

解决方案


推荐阅读