0

I am totally new at using the Combine framework. The struct I am using:

enum AuthenticateResponse: Decodable, Hashable {
    struct Success: Decodable, Hashable {
        let url, id, firstName, lastName: String
        let email, phone: String
    }

    struct Failure: Decodable, Hashable {
        struct Errors: Decodable, Hashable {
            let code: [String]?
            let username: [String]?
            let password: [String]?
        }
        let message: String
        let errors: Errors
    }

    case success(Success)
    case failure(Failure)
    init(from decoder: Decoder) throws {
        do {
            let success = try Success(from: decoder)
            self = .success(success)
        } catch {
            let failure = try Failure(from: decoder)
            self = .failure(failure)
        }
    }
}

I am getting this response from server:

failure(demo.AuthenticateResponse.Failure(
    message: "Invalid data..", 
    errors: demo.AuthenticateResponse.Failure.Errors(
        code: Optional(["The code does not exist."]), 
        username: nil, 
        password: nil)
    )
)

I am trying to get this value code: Optional(["The code does not exist."]), from above response using combine but I couldn’t

I am using below code to get that value:

func syncOnlineData() {
    print("I am here")
    viewModel.authenticateResponse
        .sink { result in
            print("result: \(result)")
        } receiveValue: { res in
            print("value: \(String(describing: res))")
        }
} 

main response from server:

["errors": {
    code =     (
        "The code does not exist."
    );
}, "message": Invalid data.]

How I will get the value from code ?

Yogurt
  • 2,913
  • 2
  • 32
  • 63
Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38
  • 1
    What does this have to do with Combine? What I mean is: how would you get that value from the server response, if you had never heard of Combine? When you show that, it will be easy to translate it into Combine. – matt Jan 19 '22 at 02:03
  • 1
    Also, your code does not actually seem to use Combine. Combine is not something you call. You create a pipeline, store it, and get out of the way, waiting for the publisher to publish. Where is your publisher? Where is your storage? What is `viewModel.authenticateResponse`? – matt Jan 19 '22 at 02:06

2 Answers2

1

The result is the AuthenticateResponse right?

If that's the case

.sink { result in 
    guard case .failure(let failure) = result else { return }

    // here is the code for each error
    print("do something for code: \(failure.errors.code)")
}
Yogurt
  • 2,913
  • 2
  • 32
  • 63
0

Biclops thanks for your help and giving me the light. If someone structure like above one this kinda best way to get your value:

.sink { result in
    switch result {
    case .none:
        print("--")
    case .some(let jsonValue):
        switch jsonValue {
        case .success(let success):
            self.verificationMessageLabel.isHidden = true
            self.continueButton.isEnabled = true
            print("success: \(success)")
        case .failure(let fail):
            guard let errorMsg = fail.errors.code else { return }
            errorMsg.forEach { errString in
                print(errString)
            }

        }
    }
}
Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38