简体   繁体   English

线程3:致命错误:索引超出范围

[英]Thread 3: Fatal error: Index out of range

I am trying to make to extract weather data but I get an out of range error. 我试图提取天气数据,但出现超出范围的错误。 can anyone help? 有人可以帮忙吗? My friend told me its a problem with my array but I was sure to use a string separator. 我的朋友告诉我这是我的数组出现问题,但是我确定要使用字符串分隔符。

My main goal is to retrieve the weather data from the website and then add it to a variable called message 我的主要目标是从网站上检索天气数据,然后将其添加到名为message的变量中

override func viewDidLoad() {
    super.viewDidLoad()
    let url = URL(string: "https://weather.weatherbug.com/weather-forecast/now/salt-lake-city")!
    let request = NSMutableURLRequest(url: url)

    let task = URLSession.shared.dataTask(with: request as URLRequest){
        data, response, error in

        var message = ""
        if  error != nil{
            print(error.debugDescription)
        }else{
            if let unwrappedData = data {
                let dataString = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue)

                var stringSeperator = "<span class=\"value ng-binding ng-scope\" ng-if=\"widget.ObservationAggregate.Observation.FeelsLike\" ng-bind=\"UnitUtility.convertTemp(widget.ObservationAggregate.Observation.FeelsLike)\">"

                if let contentArray = dataString?.components(separatedBy: stringSeperator){
                    if contentArray.count > 0 {
                       stringSeperator = "</span>"

                       let newContentArray = contentArray[1].components(separatedBy: stringSeperator)

                       if newContentArray.count > 0 {
                           message = newContentArray[0]
                           print(newContentArray[0])
                       }
                    }
                }
            }
        }
    }

    task.resume()
}

I'm assuming that you are getting this error on the following line. 我假设您在下一行遇到此错误。

let newContentArray = contentArray[1].components(separatedBy: stringSeperator)

The problem seems to be that you are checking your contentArray 's count to be greater than 0 but accessing index 1. Note here that an array's count being not 0 does not necessitate that it will have a value at index 1. Since the array's index start with 0, index 1 would in fact require your array's count to be at least 2. 问题似乎是您正在检查contentArray的计数是否大于0但正在访问索引1。请注意,此处数组的计数不为0并不一定要在索引1处有一个值。因为该数组的索引从0开始,索引1实际上会要求您数组的数量至少为2。

When we want to get some info from a server it is recommended to get/parse a JSON or an XML . 当我们想从服务器获取一些信息时,建议获取/解析JSONXML I see you are trying to parse an HTML from a website. 我看到您正在尝试从网站解析HTML

After clarifying that, if you don't have alternatives, I think you are reaching to those error: Index out of range since you are assuming your data will have always the format you think (normally you need to handle special cases) 在澄清了这一点之后,如果没有其他选择,我认为您正在解决以下error: Index out of range因为您假设数据将始终具有您认为的格式(通常需要处理特殊情况)

You could update your internal validation to this: 您可以将内部验证更新为:

if contentArray.count > 1 {
         stringSeperator = "</span>"
         let newContentArray = contentArray[1].components(separatedBy: stringSeperator)
         if newContentArray.count > 0 {
            message = newContentArray.first
            print(newContentArray.first)
         }
}

You can group those if in a better way but this is just to guide you. 您可以将它们更好地分组,但这只是为了指导您。

I hope this helps you. 我希望这可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM