【问题标题】:Swift String from an enum来自枚举的 Swift 字符串
【发布时间】:2020-06-02 15:17:00
【问题描述】:

我试图在这里获取文本以读取存储在Chat.Message.IncomingContent 枚举扩展中的privacyNotice 文本字符串的内容。以下内容此时无法编译:

private func renderPolicy(
        isEnabled: Bool,
        resource: ChosenResource<Notice>,
        message: Chat.Message.Incoming,
        ) -> Node<NodeId> {
        return Node(
            component: Component.Message(
                ....
                text: message.content.privacyNotice.text,
                ....
        )
    }

这是扩展名:

extension Chat.Message.Incoming {
    public enum Content {
        case text(message: String, style: Style)
        case response(message: String)
        case privacyNotice(text: String, resource: ChosenResource<Notice>)
    }
}

在控制台中,当我遇到断点并输入 po message.content 时,我得到以下信息:

(lldb) po message.content
▿ Content
  ▿ privacyNotice : 2 elements
    - text : "Privacy"
    ▿ resource : ChosenResource<Notice>
      - generator : (Function)

那么当我输入po message.content.privacyNotice.text 时,我应该如何处理我得到的错误,这是我期望返回的Privacy,因此我可以在renderPolicy 函数中输入这个错误:

(lldb) po message.content.privacyNotice.text
error: <EXPR>:3:9: error: enum case 'privacyNotice' cannot be used as an instance member
message.content.privacyNotice.text
~~~~~~~~^~~~~~~
        Chat.Message.Incoming.Content.

error: <EXPR>:3:31: error: value of type '(String, ChosenResource<Notice>) -> Chat.Message.Incoming.Content' has no member 'text'
message.content.privacyNotice.text
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^~~~

TIA

【问题讨论】:

  • 是否可以将您的 Content 枚举更改为 structclass

标签: swift string enums


【解决方案1】:

在你的renderPolicy函数中,message.content只是Content类型,不保证是privacyNotice,也可以是textresponse,两者都没有text 关联值。

因此,为了访问该关联值,您需要确保 message.content 实际上是大小写 privacyNotice - 您可以使用 if case ... 来做到这一点。

private func renderPolicy(
    isEnabled: Bool,
    resource: ChosenResource<Notice>,
    message: Chat.Message.Incoming,
    ) -> Node<NodeId> {
    if case .privacyNotice(text: let text, resource: _) = message.content {
        return Node(
        component: Component.Message(
            ....
            text: text,
            ....
        )
    } else {
       // handle the other casese
    }

}

【讨论】:

    【解决方案2】:

    您必须使用switch 语句并应用Pattern Matching 来确定您的text 值。

     private func renderPolicy(
        isEnabled: Bool,
        resource: ChosenResource<Notice>,
        message: Chat.Message.Incoming,
        ) -> Node<NodeId> {
    
        var nodeText: String
    
        switch message.content {
        case .response(let message):
            // you can unwrap like this or ...
            nodeText = message
        case let .privacyNotice(text, resource):
            // you can unwrap multiple wrapped values with a single 'let'
            nodeText = text
        case let .text(message, _):
            nodeText = message
        }
    
        return Node(
            component: Component.Message(
                ...
                text: nodeText,
                ....
        )
    }
    

    希望对你有所帮助,欢迎提问。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 2010-09-23
      • 1970-01-01
      • 2015-04-12
      • 2019-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多