Q: Trying to update variable in do-catch block that will be returned: fatal error: unexpectedly found nil while unwrapping an Optional value I want to return currData which is from type wData (custom type). currData is declared at the beginning of the function and will be set in a do-catch block: 
func getWData(stnID: Int) -> wData{
   var currData: wData?

   let parameters: Parameters = ["id": String(stnID), "accesstype": "w"]

   Alamofire.request("https://myurl.tld", parameters: parameters).response { response in
       print("Request: \(response.request)")
       print("Response: \(response.response)")
       print("Error: \(response.data)")

       if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
           print("Data: \(utf8Text)")

           let dArr = utf8Text.components(separatedBy: "~")
           do {
               try currData = wData(hash: Int(dArr[0])!, stnName: String(dArr[1])!)
           } catch {
               print("error creating wData Object: \(error)")
           }

       }
   }   

   return currData!
}

But when I try to return the currData object I get the following error message: 

fatal error: unexpectedly found nil while unwrapping an Optional value

My wData class looks like this: 
class wData{
     var hash: Int
     var stnName: String

     init(hash: Int, stnName: String) {
         self.hash     = hash
         self.stnName  = stnName
     }
}

I thought that I could create a static variable in my class: 
class wData{

     static var currDObj: wData?
     var hash: Int
     var stnName: String

     init(hash: Int, stnName: String) {
         self.hash     = hash
         self.stnName  = stnName

         currDObj = self
     }
}

And then return the currDObj in my getWData function with: 
return weatherData.currData!

But it still gives me an error. What point did I do wrong?

A: This line try currData = wData(hash: Int(dArr[0])!, stnName: String(dArr[1])!) will most likely not be evaluated before this line return currData! so currData is still nil. You need to use a closure with your function rather than returning the value.
Something like this: (also improved nil safety)
func getWData(stnID: Int, completion: (wData?) -> ()) {

    let parameters: Parameters = ["id": String(stnID), "accesstype": "w"]

    Alamofire.request("https://myurl.tld", parameters: parameters).response { response in
        print("Request: \(response.request)")
        print("Response: \(response.response)")
        print("Error: \(response.data)")

        if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
            print("Data: \(utf8Text)")

            let dArr = utf8Text.components(separatedBy: "~")
            guard let hash = Int(dArr[0]), stnName = String(dArr[1]) else {
                completion(nil)
                return
            }
            do {
                let currData: wData = try wData(hash: hash, stnName: stnName)
                completion(currData)

            } catch {
                print("error creating wData Object: \(error)")
                completion(nil)
            }

        }
        else {
            completion(nil)
        }
    }
}

