gpt4 book ai didi

swift - NSAttributedString 在初始化时崩溃,但为什么呢?

转载 作者:搜寻专家 更新时间:2023-10-31 23:07:12 27 4
gpt4 key购买 nike

有时这东西会崩溃,但我不知道为什么以及什么时候。有人有想法吗?

extension String {
var htmlDecoded: String? {

if let encodedData = self.data(using: String.Encoding.utf8) as Data? {
let attributedOptions = [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue] as [String : Any]
do {
let attributedString = try NSAttributedString(data: encodedData,
options: attributedOptions,
documentAttributes: nil)
return attributedString.string

} catch let error as NSError {
print("ERROR: ", error.localizedDescription)

return self
}
}
return self
}
}

这是我从 HockeyApp 得到的错误

function signature specialization <Arg[0] = Owned To Guaranteed and Exploded> of @nonobjc (extension in UIKit):__C.NSAttributedString.init(data: Foundation.Data, options: [Swift.String : Any], documentAttributes: Swift.AutoreleasingUnsafeMutablePointer<__C.NSDictionary?>?) throws -> __C.NSAttributedString (String+html.swift:0)

function signature specialization <Arg[0] = Exploded> of (extension in Podcat_2):Swift.String.htmlDecoded.getter : Swift.String? (String+html.swift:0)

最佳答案

我强烈建议你升级到 swift 4。swift 4 中的字体变化很大,最好早点进行更改。

这是我的 swift 4 版本,非常适合我将 html 字符串转换为 NSAttributedString。然后您可以调用 .string 来获取字符串本身。

extension String {
func htmlAttributedString() -> NSAttributedString? {
guard let data = self.data(using: String.Encoding.utf16, allowLossyConversion: false) else { return nil }
guard let html = try? NSMutableAttributedString(
data: data,
options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil) else { return nil }
return html
}
}

关于swift - NSAttributedString 在初始化时崩溃,但为什么呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51191069/

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