Unlike before, I was surprised to see that 'title' is now an optional (the compiler now generates the waning : String interpolation produces a debug description for an optional value; did you mean to make this explicit?).
How it comes the 'if let title =' expression does no unbox it anymore? What should I do to unbox in the if?
// Go thru all publication where the tag has been found
for item in items {
if let item = item as? [String: String?], let title = item["label"] {
i += 1
if let rawSummary = item["libSousTheme"] {
print("\(i)) Tag [\(foundTag)] z[\(foundZTag)] in « \(title) »")
}
else {
print("\(i)) Tag [\(foundTag)] z[\(foundZTag)] in « \(title) » (no summary!)")
}
}
}
title
should have been an optional prior to Swift 3.1, as you're casting to[String: String?]
, and a dictionary's subscript usesnil
to indicate the lack of a value for a key (so you get a double wrapped optional foritem["label"]
). Generally, you never want to have a dictionary with an optionalValue
type – just use a non-optionalValue
type and usenil
to indicate the lack of a value for key (i.e cast toas? [String: String]
instead). – Cannikinlet title = item["label"] as? String
and it'll work the way you're asking. – Atbara