更新: Xpre 9•Swift 4
extension String { var html2AttributedString: NSAttributedString? { do { return try NSAttributedString(data: Data(utf8), options: [.documentType: NSAttributedString.documentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil) } catch { print(error) return nil } } var unipres: [UInt32] { return unipreScalars.map{$0.value} }}Xpre 8•Swift 3
extension String { var html2AttributedString: NSAttributedString? { do { return try NSAttributedString(data: Data(utf8), options: [NSdocumentTypedocumentAttribute: NSHTMLTextdocumentType, NSCharacterEncodingdocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil) } catch { print(error) return nil } } var unipres: [UInt32] { return unipreScalars.map{$0.value} }}let str = "<span>€€</span>".html2AttributedString?.string ?? ""print(str.unipres) // [8364, 8364]



