简体   繁体   English

Swift2.0:如何通过try try获取返回值和错误

[英]Swift2.0: How to get both return value and error with do try catch

I have a function in Objective-C. 我在Objective-C中有一个函数。 This function always returns a value, event if an error occurs: 如果发生错误,此函数始终返回值,事件:

-(NSString *)test:(NSError **)error;

I override it in Swift1 as following, so that I can still get the value and the error at the same time. 我在Swift1中覆盖它如下,这样我仍然可以同时得到值和错误。

override func test(error: NSErrorPointer) -> String {
    var error: NSError?
    let result = super.test(&error)
    ...
}

But now in swift2, I can only override the function like 但是现在在swift2中,我只能覆盖像这样的函数

override func test() throws -> String {

} 

In this case, how to get the value and error at the same time? 在这种情况下,如何同时获取值和错误?

I am doing this because I need to override a function in AFNetworking. 我这样做是因为我需要覆盖AFNetworking中的一个函数。 Both return value and error are needed. 需要返回值和错误。

- (id)responseObjectForResponse:(NSURLResponse *)response
                           data:(NSData *)data
                          error:(NSError *__autoreleasing *)error;    

I know this isn't answering the question, although I could offer guidance into the way error handling works in Swift, but this might be a lot easier for you. 我知道这不是回答这个问题,虽然我可以提供有关Swift错误处理方式的指导,但这对你来说可能要容易得多。

AFNetworking has a dedicated Swift library called AlamoFire. AFNetworking有一个名为AlamoFire的专用Swift库。 If your project is in Swift, it might be beneficial for you to use the swift version of the AFNetworking library. 如果您的项目是在Swift中,那么使用快速版本的AFNetworking库可能会对您有所帮助。 It can be found here: 在这里能找到它:

https://github.com/Alamofire/Alamofire https://github.com/Alamofire/Alamofire

I think you don't really need the BOOL value returned. 我认为你真的不需要返回BOOL值。 It should be true if everything is OK and false otherwise. 如果一切正常则应该是true ,否则就是false

With Swift 2 you should use do , try and catch . 使用Swift 2你应该使用dotrycatch You can catch the error and cast it as an NSError instance. 您可以捕获错误并将其转换为NSError实例。

Example : 示例:

do {
  try something()
  // continue if something() returned true otherwise -> catch
} catch let error as NSError {
  print(error.localizedDescription)
}

With AFNetworking, you could do something like this to change the NSError localized description with an error message from the JSON : 使用AFNetworking,您可以执行类似的操作,使用JSON中的错误消息更改NSError本地化描述:

override func responseObjectForResponse(response: NSURLResponse!, data: NSData!) throws -> AnyObject {
  do {
    try self.validateResponse(response as! NSHTTPURLResponse, data: data)
  } catch let error as NSError {
    let json = JSON(data: data) // i'm using SwiftyJSON : https://github.com/SwiftyJSON/SwiftyJSON
    if let errorMessage = json["error"].string {
      var userInfo = error.userInfo
      userInfo[NSLocalizedDescriptionKey] = errorMessage
      throw NSError(domain: error.domain, code: error.code, userInfo: userInfo)
    }
  }

  return try super.responseObjectForResponse(response, data: data)
}

Maybe there is a better way to do this but i'm starting with Swift 2 :) 也许有更好的方法来做到这一点但我开始使用Swift 2 :)

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

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