I have a custom collection that can receive values of any type and converts them to strings. For example:
collection["key"] = 10
let value = collection["key"] // value is a String
Is there a way to do this? I tried implementing two subscript methods but Swift doesn't support write-only subscripts.
subscript(key: String) -> String {
get { ... }
}
// Doesn't compile
subscript(key: String) -> AnyObject {
set { ... }
}
You could also define your own type and make it conform to the
IntegerLiteralConvertibleand theStringLiteralConvertibleprotocols.Technically you could also write an extension for String to make it conform to
IntegerLiteralConvertiblebut that might get confusing, since it will be available in your entire project.