Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert return expression of type '(AnyPublisher<DemoObject, Never>, APIError)' to return type 'AnyPublisher<DemoObject, APIError>'

Tags:

swift

func testData()->AnyPublisher<DemoObject, APIError> {

    var data = DummyData().decodeClaimDetails()!
    
    return (Just(data).eraseToAnyPublisher(),
            APIError.httpError(200))
    
}

I got this error. Cannot convert return expression of type '(AnyPublisher<DemoObject, Never>, APIError)' to return type 'AnyPublisher<DemoObject, APIError>'

How can I return a type that is AnyPublisher<DemoObject, APIError>?

like image 456
JIANG Avatar asked Feb 04 '21 23:02

JIANG


1 Answers

You want

func testData()->AnyPublisher<DemoObject, APIError> {
    let d: DemoObject = // ...
    return Just<DemoObject>(d)
        .setFailureType(to: APIError.self) // <--
        .eraseToAnyPublisher()
}
like image 66
matt Avatar answered Nov 13 '22 18:11

matt