如何解决致命错误:Swift IOS 中的索引超出范围 [英] How to solve Fatal error: Index out of range in Swift IOS
问题描述
我有一个代码问题,它是适用于 IOS 的 RSS 阅读器应用程序.我有一个错误线程 1:致命错误:索引超出范围.
I have a problem with a code is a RSS reader app for IOS. I have an error Thread 1: Fatal error: Index out of range.
我在哪一行写了错误.顺便说一句,这个错误确实发生在我放置的所有 RSS 链接上,例如天空新闻 RSS URL 它运行良好,但对于某些站点(例如我现在放置的站点),它没有运行并写入致命错误:索引超出范围错误.
I wrote on which line the error is. By the way, this error does happen with all the RSS link I put for example with sky news RSS URL it works well but with some site (like the one I put now) it is not running and write Fatal error: Index out of range ERROR.
代码:
import UIKit
class FeedListViewController: UITableViewController, XMLParserDelegate {
var myFeed : NSArray = []
var feedImgs: [AnyObject] = []
var url: URL!
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140
tableView.backgroundColor = UIColorFromRGB(rgbValue: 0x00B6ED)
self.tableView.dataSource = self
self.tableView.delegate = self
loadData()
}
@IBAction func refreshFeed(_ sender: Any) {
loadData()
}
func loadData() {
url = URL(string: "https://www.widgeti.co.il/feed")!
loadRss(url);
}
func loadRss(_ data: URL) {
// XmlParserManager instance/object/variable
let myParser : XmlParserManager = XmlParserManager().initWithURL(data) as! XmlParserManager
// Put feed in array
feedImgs = myParser.img as [AnyObject]
myFeed = myParser.feeds
tableView.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "openPage" {
let indexPath: IndexPath = self.tableView.indexPathForSelectedRow!
let selectedFURL: String = (myFeed[indexPath.row] as AnyObject).object(forKey: "link") as! String
// Instance of our feedpageviewcontrolelr
let fivc: FeedItemViewController = segue.destination as! FeedItemViewController
fivc.selectedFeedURL = selectedFURL as String
}
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myFeed.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.backgroundColor = UIColor.clear
cell.detailTextLabel?.backgroundColor = UIColor.clear
if indexPath.row % 2 == 0 {
cell.backgroundColor = UIColor(white: 1, alpha: 0.1)
} else {
cell.backgroundColor = UIColor(white: 1, alpha: 0.2)
}
// Load feed iamge.
let url = NSURL(string:feedImgs[indexPath.row] as! String) //ERROR Thread 1: Fatal error: Index out of range
let data = NSData(contentsOf:url! as URL)
var image = UIImage(data:data! as Data)
image = resizeImage(image: image!, toTheSize: CGSize(width: 80, height: 80))
let cellImageLayer: CALayer? = cell.imageView?.layer
cellImageLayer!.cornerRadius = 35
cellImageLayer!.masksToBounds = true
cell.imageView?.image = image
cell.textLabel?.text = (myFeed.object(at: indexPath.row) as AnyObject).object(forKey: "title") as? String
cell.textLabel?.textColor = UIColor.white
cell.textLabel?.numberOfLines = 0
cell.textLabel?.lineBreakMode = .byWordWrapping
cell.detailTextLabel?.text = (myFeed.object(at: indexPath.row) as AnyObject).object(forKey: "pubDate") as? String
cell.detailTextLabel?.textColor = UIColor.white
return cell
}
func UIColorFromRGB(rgbValue: UInt) -> UIColor {
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
func resizeImage(image:UIImage, toTheSize size:CGSize)->UIImage{
let scale = CGFloat(max(size.width/image.size.width,
size.height/image.size.height))
let width:CGFloat = image.size.width * scale
let height:CGFloat = image.size.height * scale;
let rr:CGRect = CGRect(x: 0, y: 0, width: width, height: height)
UIGraphicsBeginImageContextWithOptions(size, false, 0);
image.draw(in: rr)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
return newImage!
}
}
你能帮我解决这个问题吗?谢谢!
Can You help me solve this problem? Thanks!
推荐答案
致命错误:索引超出范围
这个错误只涉及索引的东西,你想从数组中访问的东西不存在.
This error only refers a thing that index, you want to access from the array does not exist.
let url = NSURL(string: feedImgs[indexPath.row] as! String)
在上面的行 feedImgs[indexPath.row]
不存在,这就是为什么你会收到错误.确保您的 feedImgs
数组和 myFeed
数组的长度相同,因为您正在从 myFeed
数组加载表格.
In the above line feedImgs[indexPath.row]
does not exist, that why you are getting the error. Make sure that your feedImgs
array and myFeed
array both are of same length because you are loading the table from myFeed
array.
或者,您可以这样检查.
Or, You can can check like this.
if feedImgs.count > indexPath.row {
let url = NSURL(string: (feedImgs[indexPath.row] ?? ""))
}
这篇关于如何解决致命错误:Swift IOS 中的索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!