Incorrect format after parsing string HTML in Swift

While parsing I use this code

  func encode() -> String{
        var newStr = String(utf8String: self.cString(using: .utf8)!)
        newStr = newStr!.removingPercentEncoding

        guard let data = String(utf8String: self.cString(using: .utf8)!)?.data(using: .utf8) else {
            return newStr!
        }     
        guard let attributedString = try? NSAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding:  String.Encoding.utf8.rawValue], documentAttributes: nil) else {
            return newStr!
        }
       return  attributedString.string
    }

the problem is that it removes the \n. So I do not display the text correctly

Upvotes: 0

Views: 60

Answers (1)

Daniel Klöck
Daniel Klöck

Reputation: 21137

That's because you are using NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html

depending on what you are trying to accomplish, you may just ignore that fact or replace "\n" with something else in your String.

Upvotes: 1

Related Questions