Cann't assign variables to ExpressibleByStringLiteral?

I used xcode 10.1


enum PriceType {
    case cny
    case usd
}
extension PriceType: ExpressibleByStringLiteral {
    init(stringLiteral value: String) {
        self.init(string: value)
    }
    init(unicodeScalarLiteral value: String) {
        self.init(string: value)
    }
    init(extendedGraphemeClusterLiteral value: String) {
        self.init(string: value)
    }

    init(string: String) {
        switch string {
        case "0":
            self = .cny
        case "1":
            self = .usd
        default:
            switch Language.use {
            case .cn(.hans):
                self = "0"
            case .en, .cn(.hant):
                self = "1"
            }
        }
    }
    
    static var Symbol: String {
        let priceStr: String = "0"
        let priceType: PriceType = "priceType"
        let priceType1: PriceType = priceStr // erorr: Cannot convert value of type 'String' to specified type 'PriceType'
        return priceType.symbol
    }
    var symbol: String {
        switch self {
        case .cny:
            return ""
        case .usd:
            return "$"
        }
    }
}

when biuld, line 29 is erorr : Cannot convert value of type 'String' to specified type 'PriceType'


I hope can use :

     let priceStr: String = "0"
     let priceType1: PriceType = priceStr

How can I do? or it's cann't?

Accepted Reply

I hope can use:

That’s not going to work. Note the Literal part of the name

ExpressibleByStringLiteral
. That protocol is only involved when dealing with literals. In your second code snippet the string literal is on line 1. Once you get to line 2 you’re dealing with a string value, and thus
ExpressibleByStringLiteral
never comes into play.

To see

ExpressibleByStringLiteral
in action, you need to use a literal. For example:
let p1: PriceType = "***"

Or:

let p2 = "***" as PriceType

Those are not particularly useful examples. The

ExpressibleByStringLiteral
facility works best when you already have established the type context in play. For example:
func discount(price: PriceType) -> PriceType {
    fatalError()
}
let p3 = discount(price: "***")

The

discount(price:)
function takes a parameter of
PriceType
, and that establishes the type context that allows you to pass in a string literal.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

That is a bit convoluted, but I tested the following and it compiles:


       let priceType1: PriceType = PriceType(stringLiteral: priceStr) // erorr: Cannot convert value of type 'String' to specified type 'PriceType'

I hope can use:

That’s not going to work. Note the Literal part of the name

ExpressibleByStringLiteral
. That protocol is only involved when dealing with literals. In your second code snippet the string literal is on line 1. Once you get to line 2 you’re dealing with a string value, and thus
ExpressibleByStringLiteral
never comes into play.

To see

ExpressibleByStringLiteral
in action, you need to use a literal. For example:
let p1: PriceType = "***"

Or:

let p2 = "***" as PriceType

Those are not particularly useful examples. The

ExpressibleByStringLiteral
facility works best when you already have established the type context in play. For example:
func discount(price: PriceType) -> PriceType {
    fatalError()
}
let p3 = discount(price: "***")

The

discount(price:)
function takes a parameter of
PriceType
, and that establishes the type context that allows you to pass in a string literal.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"