简体   繁体   English

Swift function 返回多种类型

[英]Swift function to return multiple types

I'm trying to build a utility function called makePostBuilder which looks something like this.我正在尝试构建一个名为makePostBuilder的实用程序 function ,它看起来像这样。

fileprivate func makePostRequest(apiUrl: String, params: [String: String]) -> URLRequest? {
        // build url
        let urlString = "\(apiUrl)"
        guard let serviceUrl = URL(string: urlString) else { return nil }
        
        // build url request
        var request = URLRequest(url: serviceUrl)
        request.httpMethod = "POST"
        request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
        
        //set http body for url request with incoming params
        guard let httpBody = try? JSONSerialization.data(withJSONObject: params, options: []) else {
            return nil 
        }
        request.httpBody = httpBody
        return request
    }

The return type of this function is obviously incorrect.这个 function 的返回类型显然是不正确的。 I would like it to either return a URLRequest instance OR an Error instance.我希望它返回一个URLRequest实例或一个Error实例。 The error instance is mainly so the optional unwrapping can return a valuable message instead of just nil (as per the current implementation.错误实例主要是因为可选的展开可以返回有价值的消息,而不仅仅是nil (根据当前的实现。

I was thinking along the lines of a typealias , but I am not sure if that is the right approach.我在思考typealias ,但我不确定这是否是正确的方法。

//not sure if this is right
typealias CustomRequestType = (URLRequest, Error) 

At the end of the appropriate type definitions, I would like the function to look something like this在适当的类型定义结束时,我希望 function 看起来像这样


fileprivate func makePostRequest(apiUrl: String, params: [String:String]) -> CustomType {
  let urlString = apiUrl
  guard let serviceUrl = URL(string: urlString) else { return //error based on customtype? }

  // build url request
  var request = URLRequest(url: serviceUrl)
  request.httpMethod = "POST"
  request.setValue("Application/json", forHTTPHeaderField: "Content-Type")

  //set http body for url request with incoming params
  guard let httpBody = try? JSONSerialization.data(withJSONObject: params, options: []) else {
    return //error type 
  }

  //return success type
  request.httpBody = httpBody
  return request
}

I think I'm getting close, but not quite there yet.我想我已经接近了,但还没有完全达到。 I'd also love if the community could point me to some docs!如果社区可以向我指出一些文档,我也很高兴!

UPDATE: Possible Solution?更新:可能的解决方案?

//does this seem plausible?

enum DCError: String, Error {
    case invalidUrl = "the url seems to be invalid"
}

typealias DCUrlRequestType = Result<URLRequest, Error>

fileprivate func makePostRequest(apiUrl: String, params: Dictionary<String, String>) -> DCUrlRequestType {
    let urlString = apiUrl
    guard let serviceUrl = URL(string: urlString) else {
        return DCUrlRequestType.failure(DCError.invalidUrl)
    }
    
    var request = URLRequest(url: serviceUrl)
    request.httpMethod = "POST"
    request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
    
    return DCUrlRequestType.success(request)
}

There is a built-in enum Result you can use as your return type.有一个内置的enum Result可以用作返回类型。 And you can use URLError(code: .badURL) (provided by Foundation) in the case where you can't create the URL .在无法创建URL的情况下,您可以使用URLError(code: .badURL) (由 Foundation 提供)。 Thus:因此:

fileprivate func makePostRequest(apiUrl: String, params: [String: String]) -> Result<URLRequest, Error> {
    let urlString = "\(apiUrl)"
    guard let serviceUrl = URL(string: urlString) else {
        return .failure(URLError(.badURL))
    }

    var request = URLRequest(url: serviceUrl)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    do {
        request.httpBody = try JSONSerialization.data(withJSONObject: params, options: [])
    } catch {
        return .failure(error)
    }
    return .success(request)
}

BUT…但…

The natural way to write this function is to declare that it throws , like this:编写此 function 的自然方法是声明它throws ,如下所示:

fileprivate func makePostRequest(apiUrl: String, params: [String: String]) throws -> URLRequest? {
    let urlString = "\(apiUrl)"
    guard let serviceUrl = URL(string: urlString) else {
        throw URLError(.badURL)
    }

    var request = URLRequest(url: serviceUrl)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.httpBody = try JSONSerialization.data(withJSONObject: params, options: [])
    return request
}

and then let the caller use do/catch if she wants to handle the error, instead of making the caller switch over the Result cases.然后让调用者在想要处理错误时使用do/catch ,而不是让调用者切换Result案例。

If the caller really wants a Result , she can use the Result(catching:) initializer, which (with trailing closure syntax) looks like this:如果调用者真的想要一个Result ,她可以使用Result(catching:)初始化器,它(带有尾随闭包语法)如下所示:

let requestResult = Result { try makePostRequest(apiUrl: urlString, params: [:]) }

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

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