0

I have such protocols

public protocol IRouter: Requestable, MultipartUploading, Encoder {

}

where IRouter should be inheriting or conformming to both Requestable and Encoder protocol

public protocol Requestable {
    
    func asURLRequest() throws -> URLRequest
}

And MultipartUploading is another protocol with default implementation

public protocol MultipartUploading {
        
        func multipartFormData() throws -> Data?
    }
    
    public extension MultipartUploading {
        func multipartFormData() throws -> Data? { return nil }
    }

Then I add default implementation to Requestable if it is also IRouter

public extension Requestable where Self: IRouter {
    
    func asURLRequest() throws -> URLRequest {
      // make url request
    }

}

and also another implementation to MultipartUploading protocol

public extension MultipartUploading where Self: IRouter {
    
    func multipartFormData() -> Data? {
      // make data to upload
    }
}

But then If I import module to Application target and do

enum Router: IRouter {  }

It does not contain this multipartFormData and asUrlRequest implementation and fore me to implement them again? how to avoid this and have this default implementations. It seems if I do this in the same module (target) then Router: IRouter does not complain, but maybe it is just Xcode not detecting bug

1 Answer 1

1

This extensions look redundant.

public extension Requestable where Self: IRouter
public extension MultipartUploading where Self: IRouter

The thing is IRouter is a protocol, that inherits Both Requestable and MultipartUploading. So, you can replace both extensions with a single one.

public extension IRouter {
    func asURLRequest() throws -> URLRequest {
      // make url request
    }
    func multipartFormData() -> Data? {
      // make data to upload
    }
}

It should also fix your issue.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes but i consider why this does not work, as when I am testing the same code inside my framwork it works ok. There I have both Router and RouterTests targets and the same protocols, then I define MockRouter not defining this methods asURLRequest() and multipartFormData() and test whether they convert Router properties to valid results URLRequest or Data? and unit tests works and MockRouter is not complaining about not implementing above methods. I can replace like you are suggesting but I would like to know why it does not work as it I think should work
Ok I've checked it again and it works but the problem was that I've do not add tag to github repository and Xcode doesn't refresh its framework dependencies even if I think it was updated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.