【发布时间】:2020-01-10 19:50:07
【问题描述】:
我正在编写一个 tvos 应用程序,它有一个函数,我的 tvml 调用一个对象数组。
如何正确地将 json 转换为某种类型/接口的数组?
// This type is generated from my TypeScript types
struct Video: Codable {
let title: String
enum CodingKeys: String, CodingKey {
case title
}
}
{
func appController(_ appController: TVApplicationController, evaluateAppJavaScriptIn jsContext: JSContext) {
let playVideo : @convention(block) (Int, JSValue) -> Void = {
(start, json) -> Void in
// How do I properly cast the json to an array of Videos?
let playlist = json;
for video in playlist {
// here I would like to access video.title
print(video)
}
}
jsContext.setObject(unsafeBitCast(playVideo, to: AnyObject.self),
forKeyedSubscript: "playVideo" as (NSCopying & NSObjectProtocol))
}
}
PS:我是一个 swift/objc 新手。
【问题讨论】:
-
这能回答你的问题吗? How to parse Array of JSON to array in Swift
-
这似乎是从一个有点不同的文件准备好的。必须是这种 JSValue 类型(我认为)转换为 swift 类型。
标签: swift