我正在使用MWFeed(来自GitHub的Objective-C提要解析器),并且试图从提要中的文章访问图像。到目前为止,我已经写了这个:
let item = feedItems[indexPath.row] as MWFeedItem?
cell.itemImage?.layer.cornerRadius = 25.0
cell.titleLabel.text = item?.title
if item?.content != nil {
let htmlContent = item!.content as NSString
var imageSource = ""
let rangeOfString = NSMakeRange(0, htmlContent.length)
let regex = try! NSRegularExpression(pattern: "(<img.*?src=\")(.*?)(\".*?>)", options: [])
if htmlContent.length > 0 {
let match = regex.firstMatch(in: htmlContent as String, options: [], range: rangeOfString)
if match != nil {
let imageURL = htmlContent.substring(with: match!.range(at: 2)) as NSString
if NSString(string: imageURL.lowercased).range(of: "feedburner").location == NSNotFound {
imageSource = imageURL as String
}
}
}
if imageSource != "" {
cell.itemImage?.sd_setImage(with: URL(string: imageSource), placeholderImage: UIImage(named: "Placeholder1"))
}
}
编辑:item.content is nil
我发现了什么地方错了,所以代码从未被调用,有人知道为什么item.content = nil
以及如何解决这个问题吗?
这是我正在使用的let url = NSURL(string: "https://sportingnews.com/rss")