在没有延迟的情况下替换Swift中的html字符

时间:2015-08-07 06:24:26

标签: html string swift

我已经使用这个简单的扩展来删除HTML特殊字符它工作正常但问题是是否有任何其他方法可以用于干净和快速执行:

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

ex:带有特殊字符的字符串:

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

使用扩展程序后,它会替换&并返回:

   let strinWithoutCharacter = strinWithCharacter.htmlToString() 
// "Hello there & this is a test string"

0 个答案:

没有答案