With JSExport of JavaScriptCore you can convert a JavaScript object to a Swift object. However I cannot find a way how to convert an array of JavaScript objects to a native Swift array. Therefore I wanted to ask if I am missing something, or if this is just not possible.
What I've tried
const test = [{ value: 'value1' }, { value: 'value2' }]
1. NSArray
let jsValue = ...
jsValue.toObjectOf(NSArray.self)
The downsides of this approach is that I could not specify which JSExport type I want the NSArray to be. The elements of the NSArray are dictionaries. This is pretty much the same as I would get with jsValue.toArray().
2. Array
protocol TestExport: JSExport {
var value: String { get }
}
class Test: NSObject, TestExport {
let value: String
init(value: String) { self.value = value }
}
let jsValue = ...
jsValue.toObjectOf([Test].self)
This does not compile because [Test].Type is not of type AnyClass.
3. ???
Use something like https://github.com/swhitty/KeyValueCoder to parse the result of toObject() to a Codable struct. However I am not sure if this is the most performant option, since there are extra steps inbetween.