【问题标题】:Replace string text to particular text in an array string with in ios swift用 ios swift 将字符串文本替换为数组字符串中的特定文本
【发布时间】:2019-05-24 07:02:41
【问题描述】:

entities = ({confidence = "<null>"; end = 113; entity = DATE; extractor = "ner_spacy";start = 103;value = "five years"; }, {confidence = "<null>"; end = 177;entity = ORG; extractor = "ner_spacy";start = 163; value = "xyz Company"; } );

这是后端数据,我需要在字符串中显示删除并在字符串文本中添加新值:

示例:“根据您在 {{ORG}} 的 {{years_of_experience}} 经验,有哪些流程改进或标准设置?”

答案:0 的数组 ---> 五年和 1 的数组 ---> xyz 公司 我需要显示数组 0 和 1 的文本,而不是打开和关闭的大括号。

在您在 xyz 公司的五年经验中,有哪些流程改进或标准设置?

【问题讨论】:

  • 根据哪个键识别需要在字符串中输入的数据类型?
  • 另外,为您的 API 响应添加一个适当的 JSON 示例。
  • 你试过什么,你能分享你当前的代码吗?您能否还添加一个正确的示例 json 消息,您的示例中没有“years_of_experience”。

标签: swift swift3 swift3.2


【解决方案1】:

它不适用于动态数据,在某些文本中不包含任何键值和{{}},在这种情况下我们将如何编写它。

我需要用这种类型的数据显示一个表格视图并播放语音消息。

示例:q1) 您能否向我解释一下您自己,突出显示与项目经理和您从事的不同领域相关的多年经验

答案:用户说出答案,发送后端并将响应存储在字典中。

Q2) 根据您在 {{ORG}} 的 {{years_of_experience}} 经验,有哪些流程改进或标准设置? 注意:1)我需要替换 {{ }} 内的文本值 2)对于某些问题文本,没有实体键和值。 3)我们需要在里面存储 {{ORG}} 值和每当问题文本 {{ORG}} 我们应该替换实体的值。

Q3) 您能否告诉我一些软件开发方法以及您使用过和熟悉的方法?

q4) 太好了。您能说出您在 {{industry}} 领域和 {{years_of_experience}} 中支持的一些客户吗?

------------ 很快。

每当用相应的文本说出答案时,我都会存储实体键和值响应

【讨论】:

    【解决方案2】:

    我已经尝试为您的问题找到解决方案,

    这是我用作示例的JSON response

    [
        {
            "confidence": "<null>",
            "end": 113,
            "entity": "DATE",
            "extractor": "ner_spacy",
            "start": 103,
            "value": "five years"
        },
        {
            "confidence": "<null>",
            "end": 177,
            "entity": "ORG",
            "extractor": "ner_spacy",
            "start": 163,
            "value": "xyz Company"
        }
    ]
    

    使用CodableJSON response 解析为array of Entity 对象,即

    struct Entity: Codable {
        var confidence: String?
        var end: Int?
        var entity: String?
        var extractor: String?
        var start: Int?
        var value: String?
    }
    

    我在响应中使用了entity key 来确定要替换的值,即

    if let data = str.data(using: .utf8) { //You'll get this data from API response
        let entities = try? JSONDecoder().decode([Entity].self, from: data)
    
        var sentence = "In your {{DATE}} of experience at {{ORG}}, what kind of process improvements or standards setup?"
        entities?.forEach({
            if let entity = $0.entity, let value = $0.value {
                sentence = sentence.replacingOccurrences(of: "{{\(entity)}}", with: value)
            }
        })
        print(sentence) //In your five years of experience at xyz Company, what kind of process improvements or standards setup?
    }
    

    在上面的代码中,我遍历了entities array,并将每次出现的{{entity}}替换为各自的value,即

    "{{DATE}}" is replaced with "five years"
    "{{ORG}}" is replaced with "xyz Company"
    

    如果您仍然遇到任何问题或者我没有很好地理解问题陈述,请告诉我。

    【讨论】:

    • Codable 据我所知,JSONDecoder 是 Swift 4。
    • @JoakimDanielson 是的,没错。没有看到 Swift-3 的要求。
    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 2014-06-21
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多