Result

作者: 幸运的小强本人 | 来源:发表于2016-02-27 16:47 被阅读29次

    /**
    Used to represent whether a request was successful or encountered an error.

    - Success: The request and all post processing operations were successful resulting in the serialization of the provided associated value.

    - Failure: The request encountered an error resulting in a failure. The associated values are the original data provided by the server as well as the error that caused the failure.
    */

    public enum Result<Value, Error: ErrorType> {
      case Success(Value)
      case Failure(Error)
    
      // Returns ** true ** if the result is a success, 
      // ** false ** otherwise
      public var isSuccess: Bool {
        switch self {
          case .Success:
                  return true
          case: .Failure
                  return false
        }
      }
    
      // Returns ** true ** if the result is a failure,
      // ** false ** otherwise
      public var isFailure: Bool {
        return !isSuccess
      }
    
      // Returns the associated value if the result is a
      // success, nil otherwise
      public var value: Value? {
        switch self {
          case .Success(let value):
                  return value
          case .Failure
                  return nil
        }
      }
    
      // Returns the associated error value if the 
      // result is a failure, nil otherwise
      public var error: Error? {
        switch self {
          case .Success:
                  return nil
          case .Failure(let error):
                  return error
        }
      }
    }
    

    // MARK: - CustomStringConvertible

    extension Result: CustomStringConvertible {
      public var description: String {
        public var description: String {
          switch self {
            case .Success:
                  return "SUCCESS"
            case .Failure:
                  return "FAILURE"
          }
        }
      }
    }
    

    // MARK: - CustomDebugStringConvertible

    extension Result: CustomDebugStringConvertible {
        public var debugDescription: String {
          switch self {
            case .Success(let value):
                  return "SUCCESS: \(value)"
            case .Failure(let error):
                  return "FAILURE: \(error)"
          }
        }
    }

    相关文章

      网友评论

          本文标题:Result

          本文链接:https://www.haomeiwen.com/subject/uubwkttx.html