I've the problem as Mostafa Essam stated. Don't know how to apply the suggested solution. Help is appreciated.
This is my code
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "NieuwsCollectionViewControllerCell", for: indexPath) as! NieuwsCollectionViewCell
let feedRow = MyNewsLoader.shared.returnNews()[indexPath.row]
let feedTitle = feedRow.title
let feedDescription = feedRow.description?.trimHTMLTags()
let feedLink = feedRow.link
cell.feedTitle?.text = feedTitle
cell.feedDescription?.text = feedDescription
return cell
}
When I remove ?.trimHTMLTags there's no crash.
This is the code of trimHTMLTags which causes the app to crash:
extension String {
public func trimHTMLTags() -> String? {
//remove <figure>
var textZonderFigure = ""
let textZonderFigureArray = self.components(separatedBy: "</figure>")
//print(textZonderFigureArray)
if textZonderFigureArray.count == 1 {
//there was no <figure>
textZonderFigure = textZonderFigureArray[0]
} else {
textZonderFigure = textZonderFigureArray[1]
}
guard let htmlStringData = textZonderFigure.data(using: String.Encoding.utf8) else {
return nil
}
//print(htmlStringData)
let options: [NSAttributedString.DocumentReadingOptionKey : Any] = [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue
]
let attributedString = try? NSAttributedString(data: htmlStringData, options: options, documentAttributes: nil)
let returnValue = attributedString?.string
//in returnValue zitten soms \n bij de start
//print(returnValue)
return returnValue
}
}