处理Swift中的nil值

时间:2015-05-21 17:09:49

标签: ios swift null

NSString(data: data, encoding: NSUTF8StringEncoding)返回nil。 (正确)如果值为nil,我需要将变量stringResponse设置为""否则返回NSString(data: data, encoding: NSUTF8StringEncoding)。我是这样做的:

override func start() {
    if self.cancelled {
        return
    }

    var stringResponse: String?
    var urlString: String!
    if self.arduinoConnection.arduinoHTTPPort == 80 {
        urlString = String(format: "http://%@/arduino/%@/%i/", arguments: [self.arduinoConnection.arduinoAddress, self.arduinoConnection.pinType.lowercaseString, self.arduinoConnection.arduinoPin])
    } else {
        urlString = String(format: "http://%@:%i/arduino/%@/%i/", arguments: [self.arduinoConnection.arduinoAddress, self.arduinoConnection.arduinoHTTPPort, self.arduinoConnection.pinType.lowercaseString, self.arduinoConnection.arduinoPin])
    }
    //let url = NSURL(string: urlString)
    let url = NSURL(string: "http://10.0.3.9:8888/index.html")
    let request = NSURLRequest(URL: url!)

    if self.cancelled {
        return
    }


    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in

        stringResponse = NSString(data: data, encoding: NSUTF8StringEncoding) as? String ?? ""

        if (stringResponse == "") {
            self.arduinoConnection.state = .Failed
            self.arduinoConnection.switchState = false
            self.arduinoConnection.statusText = "Failed To Connect"

            self.delegate?.didFinishDownloading(GettingState.Failed, statusText: "Failed To Connect", switchState: false)
        } else {
            self.arduinoConnection.state = .Downloaded
            if (stringResponse == "0") {
                self.arduinoConnection.state = .Downloaded
                self.arduinoConnection.switchState = false
                self.ardCTS.state = .Downloaded
                self.ardCTS.switchState = false
                self.arduinoConnection.statusText = "Connected"

                self.delegate?.didFinishDownloading(GettingState.Downloaded, statusText: "Connected", switchState: false)
            } else if (stringResponse == "1") {
                self.arduinoConnection.state = .Downloaded
                self.arduinoConnection.switchState = true
                self.arduinoConnection.statusText = "Connected"
                self.ardCTS.state = .Downloaded
                self.ardCTS.switchState = true

                self.delegate?.didFinishDownloading(GettingState.Downloaded, statusText: "Connected", switchState: true)
            }
        }
        self.executing = false
        self.finished = true
    }
    executing = true
    self.finished = false
}

当app运行此代码时,我有:

fatal error: unexpectedly found nil while unwrapping an Optional value

请帮帮我!

1 个答案:

答案 0 :(得分:2)

var stringResponse: String?

NSURLConnection.sendAsynchronousRequest(request,
                         queue: NSOperationQueue.mainQueue()) {(response, data, error) in

    stringResponse = NSString(data: data, encoding: NSUTF8StringEncoding) as? String ?? ""

您没有测试错误。您可能会传递nil NSData,因为Apple尚未对此界面进行全面审核,仍然会隐式传递未解包的选项。你必须处理那些。

if let error = error {
    // We got an error; do something about it
    return
}

// OK, now we know that the data is good
stringResponse = NSString(data: data, encoding: NSUTF8StringEncoding)

你不应该把它当作任何东西。此函数返回String?,这是stringResponse