我正在尝试访问APIKeyObjects中的描述键,但是通过语法我遇到了很多问题。
struct NewsApiObject: Codable
{
let status: String?
let totalResults: Int?
let articles: [APISKeyObjects]
}
struct Source: Codable
{
var id: String?
var int: Int?
}
struct APISKeyObjects: Codable
{
let source: Source?
let author: String?
let title: String?
let description: String?
let url: String?
let urlToImage: String?
let publishedAt: String?
let content: String?
}
当我尝试分配“描述”(在我的结构中名为“ APIKeyObjects”的可选字符串)中收到以下错误消息
错误消息:“无法为索引类型为'String'的[APIKeyObjects]类型下标”
let financesNewsData = try JSONDecoder().decode(NewsApiObject.self, from: data)
在我尝试将变量分配给名为UITextDisplay的UITextView属性之后,该属性仅接受String。
UITextDisplay.text = financesNewsData.articles[description]
错误消息:“无法为索引类型为'String'的[APIKeyObjects]类型下标”
所以我的问题是;将字符串值分配给字符串属性的正确方法是什么?
答案 0 :(得分:0)
这是错误的String var
,应该是var variableName = ...
在您的情况下,应该是:
//要获取所有文章:
var articles = financesNewsData.articles
//如果您想获取文章的说明,
let description = articles[index].desccription