Here\'s one,
import SQLite
var r:[[Any]] = []
do {
if let stmt = try local.db?.prepare(q) {
r = Array(stmt)
}
You're using Array
's sequence initialiser, which has the signature:
init<S>(_ s: S) where S : Sequence, Element == S.Iterator.Element
Because you typed r
as [[Any]]
, Element
is [Any]
. However, the sequence you're passing in has an Iterator.Element type of [Binding?]. Therefore, you're implicitly coercing Binding?
to Any
, and as per SE-0140, this will invoke a warning – as you're losing the optionality of the inner elements, which is potentially undesirable.
As the proposal says, one way to silence this warning is to add an explicit as Any
cast. In your case, this can be achieved by using a nested map(_:):
r = stmt.map { $0.map { $0 as Any } }
This shouldn't be any more costly than using Array
's sequence initialiser due to the fact that a walk over all the inner elements will have to be done in either case, due to the difference in how Swift stores abstract-typed values (see this Q&A for more info).
However, really you should be asking yourself whether r
should be of type [[Any]]
. I see no reason why you shouldn't just type it as [[Binding?]]
. Doing so will both get rid of the warning and give you better type-safety.