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

时间:2021-08-05 16:23:07

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()
}

2 个解决方案

#1


0  

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。

#2


0  

When we want to get some info from a server it is recommended to get/parse a JSON or an XML. I see you are trying to parse an HTML from a website.

当我们想从服务器获取一些信息时,建议获取/解析JSON或XML。我看到你正在尝试解析网站上的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)

在澄清之后,如果你没有替代方案,我认为你正在达到这些错误:索引超出范围,因为你假设你的数据总是你想的格式(通常你需要处理特殊情况)

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.

我希望这可以帮助你。

#1


0  

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。

#2


0  

When we want to get some info from a server it is recommended to get/parse a JSON or an XML. I see you are trying to parse an HTML from a website.

当我们想从服务器获取一些信息时,建议获取/解析JSON或XML。我看到你正在尝试解析网站上的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)

在澄清之后,如果你没有替代方案,我认为你正在达到这些错误:索引超出范围,因为你假设你的数据总是你想的格式(通常你需要处理特殊情况)

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.

我希望这可以帮助你。