From 2469622a511abc3228166850e983385c6df1e7d6 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 18 Aug 2018 16:07:34 -0400 Subject: [PATCH] Fix crash on invalid URL --- Tusker/Views/StatusTableViewCell.swift | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Tusker/Views/StatusTableViewCell.swift b/Tusker/Views/StatusTableViewCell.swift index f754b915..a399e7ea 100644 --- a/Tusker/Views/StatusTableViewCell.swift +++ b/Tusker/Views/StatusTableViewCell.swift @@ -38,9 +38,9 @@ class StatusTableViewCell: UITableViewCell { let doc = try! SwiftSoup.parse(status.content) let body = doc.body()! - print("---") - print(status.content) - print("---") +// print("---") +// print(status.content) +// print("---") let text = attributedTextForNode(body) contentLabel.attributedText = text @@ -74,13 +74,16 @@ class StatusTableViewCell: UITableViewCell { case "br": attributed.append(NSAttributedString(string: "\n")) case "a": - let linkRange = NSRange(location: 0, length: attributed.length) - let linkAttributes: [NSAttributedString.Key: Any] = [ - .foregroundColor: UIColor.blue - ] - attributed.setAttributes(linkAttributes, range: linkRange) - let link = try! node.attr("href") - links[linkRange] = URL(string: link)! + if let link = try? node.attr("href"), + let url = URL(string: link) { + let linkRange = NSRange(location: 0, length: attributed.length) + let linkAttributes: [NSAttributedString.Key: Any] = [ + .foregroundColor: UIColor.blue + ] + attributed.setAttributes(linkAttributes, range: linkRange) + + links[linkRange] = url + } default: break }