gpt4 book ai didi

swift - 在 Swift 中解析字符串 HTML 后格式不正确

转载 作者:行者123 更新时间:2023-11-28 14:20:55 25 4
gpt4 key购买 nike

在解析时我使用这段代码

  func encode() -> String{
var newStr = String(utf8String: self.cString(using: .utf8)!)
newStr = newStr!.removingPercentEncoding

guard let data = String(utf8String: self.cString(using: .utf8)!)?.data(using: .utf8) else {
return newStr!
}
guard let attributedString = try? NSAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil) else {
return newStr!
}
return attributedString.string
}

问题是它删除了\n。所以我没有正确显示文本

最佳答案

那是因为你正在使用 NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html

根据您要完成的目标,您可以忽略该事实或将 "\n" 替换为 String 中的其他内容。

关于swift - 在 Swift 中解析字符串 HTML 后格式不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51947193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com