I am pulling a JSON file from a site and one of the strings received is:
The Weeknd ‘King Of The Fall&
Swift 4
extension String {
var replacingHTMLEntities: String? {
do {
return try NSAttributedString(data: Data(utf8), options: [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
], documentAttributes: nil).string
} catch {
return nil
}
}
}
Simple Usage
let clean = "Weeknd ‘King Of The Fall’".replacingHTMLEntities ?? "default value"