I have a struct that has a method to return a dictionary representation. The member variables were a combination of different types (String and Double?)
With the following code example, there would be a warning from Xcode (Expression implicitly coerced from 'Double?' to Any)
struct Record {
let name: String
let frequency: Double?
init(name: String, frequency: Double?) {
self.name = name
self.frequency = frequency
}
func toDictionary() -> [String: Any] {
return [
"name": name,
"frequency": frequency
]
}
}
However if it was returning a type [String: Any?], the warning goes away:
struct Record {
let name: String
let frequency: Double?
init(name: String, frequency: Double?) {
self.name = name
self.frequency = frequency
}
func toDictionary() -> [String: Any?] {
return [
"name": name,
"frequency": frequency
]
}
}
My question is: Is this correct? And if it is, can you point me to some Swift documentation that explains this?
If it isn't, what should it be?
== EDIT ==
The following works too:
struct Record {
let name: String
let frequency: Double?
init(name: String, frequency: Double?) {
self.name = name
self.frequency = frequency
}
func toDictionary() -> [String: Any] {
return [
"name": name,
"frequency": frequency as Any
]
}
}