简体   繁体   English

Swift NSURLSession App在蜂窝数据上崩溃

[英]Swift NSURLSession App crashes on cellular data

I am trying to get a value from a website with the following code, it works fine when I test the app on my phone on WiFi but when I am on cellular data, the app crashes with error array index out of range After doing some debugging, it looks like calling componentsSeparatedByString works over WiFi but not over cellular data (it does not create the array as it should) 我正在尝试使用以下代码从网站获取值,当我在手机上通过WiFi测试手机上的应用程序时,它工作正常,但是当我使用蜂窝数据时,应用程序崩溃,错误array index out of range ,则似乎调用componentsSeparatedByString可以通过WiFi进行操作,但不能通过蜂窝数据进行操作(它不会按原样创建数组)

import UIKit
import Foundation

class ViewController: UIViewController {

@IBOutlet weak var goldPriceLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    var urlString = "http://www.cookson-clal.com/cours/"
    var url = NSURL(string: urlString)

    var defaultConfigObject = NSURLSessionConfiguration.defaultSessionConfiguration()
    defaultConfigObject.allowsCellularAccess = true

    var session = NSURLSession(configuration: defaultConfigObject)
    let task = session.dataTaskWithURL(url!) {(data, response, error) in

        var pageCode = NSString(data: data, encoding:NSUTF8StringEncoding)!

        var contentArray = pageCode.componentsSeparatedByString("<td width=\"16%\" align=\"right\"  class=\"indx_4\">")

        var newContentArray = contentArray[1].componentsSeparatedByString("</td>")

        var goldPriceString = (newContentArray[0].stringByReplacingOccurrencesOfString("€", withString: "").stringByReplacingOccurrencesOfString(",", withString: "."))

        var ASCIIgoldPrice = String()

        for tempChar in goldPriceString.unicodeScalars {
            if (tempChar.isASCII()) {
            ASCIIgoldPrice.append(tempChar)
            }
        }

        var goldPrice:Float = (ASCIIgoldPrice as NSString).floatValue

        dispatch_async(dispatch_get_main_queue()) {

            self.goldPriceLabel.text = "\(goldPrice)"

        }
        println(goldPrice)
    }


    task.resume()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

What you're doing is web scraping which is inherently unstable, particularly the way you're doing it. 您正在做的是网页抓取 ,它固有地不稳定,尤其是您的操作方式。 There is no guarantee that the content returned from that url will match up with the precise text you're using to break up the html. 无法保证从该url返回的内容将与您用来拆分html的精确文本匹配。 You've already found that you get different responses depending on the connection type. 您已经发现根据连接类型会得到不同的响应。 What if they restyle the page? 如果他们重新设置页面样式怎么办?

Move to a service with a published API, or be a bit more liberal with your parsing (but don't use regex ) 转到使用已发布的API的服务,或者在解析时更加自由(但不要使用regex

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

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