NetworkOperationResult

struct NetworkOperationResult
extension NetworkOperationResult : Equatable, NetworkResult

Basic implementation for handling the result of NetworkOperation action. The primary requirement for this class is a NetworkStatus result value. Data and status codes are all optional

  • A network status code often used to hold a value such as a 200 or 404 as found on typical HTTP operations. If this value is -1 it is deemed not in use.

    Declaration

    Swift

    var code: Int
  • Any data associated with the result; can be in any format

    Declaration

    Swift

    var data: Data?
  • Initializes a new NetworkOperationResult object.

    Declaration

    Swift

    init(_ status: NetworkStatus, data: Data? = nil, statusCode: Int = -1)
  • This function makes working with JSON data a bit easier, when it is known that the data represented in the .data property is JSONDecodable, the instance function jsonDecode will perform that work for you by simply having you supply the type to decode it into. Rather than throwing, it will provide you an option of the specified type with a nil result if any errors occurred.

    struct Name: Codable {
      var first: String
      var last: String
    }
    
    let result = NetworkOperationResult(...)
    let name = result.jsonDecode(Name.self)
    print("\(name.last), \(name.first)")
    

    Declaration

    Swift

    func jsonDecode<T>(_ type: T.Type) -> T? where T : Decodable

    Parameters

    type

    typically the class or struct name followed by .self

    Return Value

    an optional of the supplied Decodable object; either nil or populated with the contents of the .data property.

  • A status flag indicating whether the result was successful, a failure or in progress

    Declaration

    Swift

    var status: NetworkStatus