0

I have used this simple extension to remove HTML special charactes it works fine but the problem is its slow is there any other method which can be used for clean and fast execution:

extension String {
  func htmlToString() -> String {
    return NSAttributedString(data: dataUsingEncoding(NSUTF8StringEncoding)!, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil, error: nil)!.string
  }
}

ex: string with special character:

let strinWithCharacter = "Hello there & this is a test string"

after using extension it replaces & and return:

   let strinWithoutCharacter = strinWithCharacter.htmlToString() 
// "Hello there & this is a test string"
Hamza Ansari
  • 3,009
  • 1
  • 23
  • 25

0 Answers0