on Swift String
is a struct and you could just initialize it using
var someString:String = \"Hello\"
how woul
This is (in my opinion) neat part of the language. Yes, this is possible, thanks to the ExpressibleByStringLiteral protocol.
Unfortunately, there is some complexity to it. ExpressibleByStringLiteral inherits from ExpressibleByExtendedGraphemeClusterLiteral, which itself inherits from ExpressibleByUnicodeScalarLiteral. Thus, to conform to the first, you must conform to the other 2 above it.
This makes it possible for your struct or class to be initialized from:
UnicodeScalarLiteralType
(such as a UnicodeScalar
, which is a single Unicode code point, e.g. "A"
)ExtendedGraphemeClusterLiteralType
(such as a Character
, which is a collection of UnicodeScalar
s, such as "