79107867

Date: 2024-10-20 19:18:56
Score: 3.5
Natty:
Report link

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

    }
    
}
Reasons:
  • Blacklisted phrase (1): appreciated
  • RegEx Blacklisted phrase (3): Help is appreciated
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Low reputation (0.5):
Posted by: Patrick Koning