Swift struct initialization, making another struct like String

前端 未结 1 1270
误落风尘
误落风尘 2020-12-11 23:27

on Swift String is a struct and you could just initialize it using

var someString:String = \"Hello\"

how woul

相关标签:
1条回答
  • 2020-12-11 23:46

    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:

    • A UnicodeScalarLiteralType (such as a UnicodeScalar, which is a single Unicode code point, e.g. "A")
    • An ExtendedGraphemeClusterLiteralType (such as a Character, which is a collection of UnicodeScalars, such as "
    0 讨论(0)
提交回复
热议问题