swift - 为什么 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)
}
}
解决方案
推荐阅读
- php - 具有多个参数的 Laravel 自定义验证规则
- reactjs - React-router 使用正则表达式在 URL 更改时重新渲染
- java - 无论如何在java中运行时更改对象类?
- kubernetes - 如何获取可用于 HPA 的指标列表?
- hadoop - 安装 hadoop-2.9.1 和 jdk1.8.0.111 的问题
- javascript - 用于 Rails 条件的 Javascript 输出
- .net-core - 无法从 localhost 连接到 Redis
- javascript - javascript 的最新 woocomerce api rest 客户端出现 401 错误
- c# - 如何正确地从 long 转换为 int?
- r - 通过ggplot将直方图更改为线图