gpt4 book ai didi

swift - 如何在 swift 中接受 RateController 的评级值作为字符串

转载 作者:行者123 更新时间:2023-11-30 13:21:18 25 4
gpt4 key购买 nike

我正在从 API 中以字符串数组的形式获取评级数据。但“RatingControl.Rating”值只接受 Int 值。因此,当在自定义表格 View 单元格中填充评级时,它会给出错误。如何解决?

评级声明:

   var Ratings:Array< String > = Array < String >()

现在我从 Api 获取数据:

        var json: NSArray!
do {
json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions()) as? NSArray
} catch {
print(error)
}

self.Ratings = json.valueForKey("rate") as! Array<String>
print(self.Ratings)

它成功地向我展示了 Api 的值(value)。现在我将其填充到自定义 TableView 单元中。

      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell =
tableView.dequeueReusableCellWithIdentifier("ReviewCustomCell",
forIndexPath: indexPath) as! ReviewCustomCell

**cell.ratingControl.rating = Ratings[indexPath.row]**
return cell
}

上面的粗体显示错误为“无法接受字符串类型的值到 int”。任何人都可以提出解决这个问题的想法。

最佳答案

让我们尝试将评级值作为整数数组而不是字符串数组。

 // var Ratings:Array< String > = Array < String >()
var Ratings:Array<Int > = Array<Int >()

检查是否有效

关于swift - 如何在 swift 中接受 RateController 的评级值作为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37752123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com